在服务器端出现异常,或者客户端请求出错时,直接返回异常信息对用户来说是非常不友好的,我们需要对异常信息进行统一处理
1、使用 @ControllerAdvice 注解
使用 @ControllerAdvice 注解的控制层的全局统一异常处理
@ControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)public ModelAndView customException(Exception e) {ModelAndView mav = new ModelAndView();// 跳转到错误页面mav.setViewName("error");return mav;}
}
@ControllerAdvice 表示这是一个控制器增强类,当控制器发生异常且符合类中定义的拦截异常类,将会被拦截
@ExceptionHandler 则是定义拦截的异常类,可以是自定义异常类,例如:
@ExceptionHandler(value = MyException.class)
@ResponseBody
public JSONObject myErrorHandler(MyException exception) {JSONObject res = new JSONObject();res.put("code", exception.getCode());res.put("msg", exception.getMsg());return res;
}
当然,我们可以不返回页面,只返回异常信息
使用 @ControllerAdvice 只能捕获 Controller 层的异常,前提是 Controller 层没有对异常做 catch 处理
2、实现 ErrorController 接口
以下是网上一位博主给出的示例代码,博客地址为:https://blog.csdn.net/king_is_everyone/article/details/53080851
@Controller
@RequestMapping(value = "error")
@EnableConfigurationProperties({ServerProperties.class})
public class ExceptionController implements ErrorController {private ErrorAttributes errorAttributes;@Autowiredprivate ServerProperties serverProperties;/*** 初始化ExceptionController** @param errorAttributes*/@Autowiredpublic ExceptionController(ErrorAttributes errorAttributes) {Assert.notNull(errorAttributes, "ErrorAttributes must not be null");this.errorAttributes = errorAttributes;}/*** 定义404的ModelAndView** @param request* @param response* @return*/@RequestMapping(produces = "text/html", value = "404")public ModelAndView errorHtml404(HttpServletRequest request, HttpServletResponse response) {response.setStatus(getStatus(request).value());Map model = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML));return new ModelAndView("error/404", model);}/*** 定义404的JSON数据** @param request* @return*/@RequestMapping(value = "404")@ResponseBodypublic ResponseEntity
总结
1、@ControllerAdvice 定义多个拦截方法,拦截不同的异常类,并且可以获取抛出的异常信息
2、@ControllerAdvice 只能捕获进入 Controller 层的异常,比如404,401等错误无法捕获
3、ErrorController,该方式几乎可以处理所有的异常,包括未进入控制器的错误,比如404,401等错误
当然,前提是没有对异常做 catch 处理才能捕获到