Spring 异常处理的各种姿势

时间:2020-09-16 12:41:29   收藏:0   阅读:55
1. 前言

统一的异常处理对于应用的重要性不言而喻。今天我们来介绍一下 Spring 如何来进行统一的 Rest 异常处理。同时我们也会简单比较一下它们之间的优劣。

2. @Controller 结合 @ExceptionHandler

在控制器中声明一个方法然后用 @ExceptionHandler 注解标记即可:

@Controller
 @RequestMapping("/test")
 public class TestController {

     @RequestMapping("/err")
     @ResponseBody
     public Object demo1(){
         int i = 1 / 0;
         return new Date();
     }

     @ExceptionHandler({RuntimeException.class})
     public ModelAndView fix(Exception ex){
         System.out.println(ex.getMessage());
         return new ModelAndView("error",new ModelMap("ex",ex.getMessage()));
     }
 }

优点:

缺点:

3. @ControllerAdvice 结合 @ExceptionHandler

这是 2. 的改进型,通过定义 @ControllerAdvice 类并在方法上标记 @ExceptionHandler ,达到了全局异常处理的目的:

@ControllerAdvice
 public class TestController {

     @ExceptionHandler({RuntimeException.class})
     public ModelAndView fix(Exception ex){
         System.out.println(ex.getMessage());
         return new ModelAndView("error",new ModelMap("ex",ex.getMessage()));
     }
 }

优点:

缺点:

一般情况下也建议使用该方式进行异常处理。大多数情况下都是兼容的。

4. HandlerExceptionResolver 接口

实现 HandlerExceptionResolver 接口,这里我们继承其抽象实现 AbstractHandlerExceptionResolver :

@Component
 public class RestResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver {

     @Override
     protected ModelAndView doResolveException(
       HttpServletRequest request,
       HttpServletResponse response,
       Object handler,
       Exception ex){
         try {
             if (ex instanceof IllegalArgumentException) {
                 return handleIllegalArgument((IllegalArgumentException) ex, response, handler);
             }
            //todo more exception
         } catch (Exception handlerException) {
               //todo
         }
         return null;
     }

     private ModelAndView
       handleIllegalArgument(IllegalArgumentException ex, HttpServletResponse response)
       throws IOException {
         response.sendError(HttpServletResponse.SC_CONFLICT);
         String accept = request.getHeader(HttpHeaders.ACCEPT);
           //todo  more  response
         return new ModelAndView();
     }
 }

优点:

缺点:

5. Spring Boot 中的异常处理

如果你用的框架是 Spring Boot 。 我们还可以用它独特的处理方式。优点是屏蔽了低级的API,缺点也比较明显,无法捕捉到具体的异常。

5.1 实现 ErrorController

Spring Boot 在默认情况下,提供了 /error 映射来处理所有错误,在 Servlet 容器里注册了全局的错误页面(Whitelabel Error Page)并返回客户端。通过实现 ErrorController 接口并注册为 Bean。这里不再举例。可参考 BasicErrorController

5.2 添加 ErrorAttributes

我们也可以添加 ErrorAttributes 类型的 Bean 来替换替换默认的异常处理。

@Component
 public class MyCustomErrorAttributes extends DefaultErrorAttributes {

     @Override
     public Map<String, Object> getErrorAttributes(
       WebRequest webRequest, boolean includeStackTrace){
         Map<String, Object> errorAttributes =
           super.getErrorAttributes(webRequest, includeStackTrace);
         errorAttributes.put("locale", webRequest.getLocale()
             .toString());
         errorAttributes.remove("error");

         //todo your business

         return errorAttributes;
     }
 }

5.3 继承基类 BasicErrorController

Spring Boot 自动配置还提供了实现 ErrorController 接口异常处理的基类 BasicErrorController,默认是处理 text/html类型请求的错误,可以继承该基类自定义处理更多的请求类型,添加公共方法并使用 @RequestMapping 注解的 produce属性指定处理类型。

@Component
 public class MyErrorController extends BasicErrorController {

     public MyErrorController(ErrorAttributes errorAttributes) {
         super(errorAttributes, new ErrorProperties());
     }

     @RequestMapping(produces = MediaType.APPLICATION_XML_VALUE)
     public ResponseEntity<Map<String, Object>> xmlError(HttpServletRequest request) {

     //todo your business

     }
 }

6. Spring 5 的 ResponseStatusException

另外在最新的 Spring 5 中你还可以通过 抛出 ResponseStatusException 异常来进行处理。

好处:

缺点:

7. 总结

我们对常用的、不常用的 Spring 处理异常的方式进行了总结和优劣上的分析。 相信你可以从中找到适合你的处理方式。如果对你有用请帮忙点一个赞,您的鼓励,我的动力!

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!