@ControllerAdvice
public class ExceptionResolver {
Logger logger = LoggerFactory.getLogger(ExceptionResolver.class);
@ExceptionHandler(RuntimeException.class)
public Object exceptionHandler(RuntimeException ex, HttpServletResponse response) {
MappingJackson2JsonView view = new MappingJackson2JsonView();
Map<String, Object> map = new HashMap<>();
logger.error(ex.getMessage(), ex);
if (ex instanceof GlobalBusinessException) {
GlobalBusinessException businessException = (GlobalBusinessException) ex;
map.put("code", businessException.getStatus().getCode());
map.put("msg", businessException.getStatus().getMsg());
return new ModelAndView(view, map);
} else if (ex instanceof RuntimeException) {
map.put("code", GlobalResultStatus.ERROR.getCode());
map.put("msg", "失败");
return new ModelAndView(view, map);
} else {
map.put("code", GlobalResultStatus.ERROR.getCode());
map.put("msg", "系统异常,请联系管理员");
return new ModelAndView(view, map);
}
}
}
public class ExceptionResolver {
Logger logger = LoggerFactory.getLogger(ExceptionResolver.class);
@ExceptionHandler(RuntimeException.class)
public Object exceptionHandler(RuntimeException ex, HttpServletResponse response) {
MappingJackson2JsonView view = new MappingJackson2JsonView();
Map<String, Object> map = new HashMap<>();
logger.error(ex.getMessage(), ex);
if (ex instanceof GlobalBusinessException) {
GlobalBusinessException businessException = (GlobalBusinessException) ex;
map.put("code", businessException.getStatus().getCode());
map.put("msg", businessException.getStatus().getMsg());
return new ModelAndView(view, map);
} else if (ex instanceof RuntimeException) {
map.put("code", GlobalResultStatus.ERROR.getCode());
map.put("msg", "失败");
return new ModelAndView(view, map);
} else {
map.put("code", GlobalResultStatus.ERROR.getCode());
map.put("msg", "系统异常,请联系管理员");
return new ModelAndView(view, map);
}
}
}