阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Spring Boot统一异常处理的拦截指南

32次阅读
没有评论

共计 3487 个字符,预计需要花费 9 分钟才能阅读完成。

导读 通常我们在 Spring Boot 中设置的统一异常处理只能处理 Controller 抛出的异常。有些请求还没到 Controller 就出异常了,而这些异常不能被统一异常捕获,例如 Servlet 容器的某些异常。

Spring Boot 统一异常处理的拦截指南
通常我们在 Spring Boot 中设置的统一异常处理只能处理 Controller 抛出的异常。有些请求还没到 Controller 就出异常了,而这些异常不能被统一异常捕获,例如 Servlet 容器的某些异常。今天我在项目开发中就遇到了一个,这让我很不爽,因为它返回的错误信息格式不能统一处理,我决定找个方案解决这个问题。

ErrorPageFilter

Spring Boot 统一异常处理的拦截指南
Whitelabel Error Page

这类图相信大家没少见,Spring Boot 只要出错,体现在页面上的就是这个。如果你用 Postman 之类的测试出了异常则是:

{ 
  "timestamp": "2021-04-29T22:45:33.231+0000", 
  "status": 500, 
  "message": "Internal Server Error", 
  "path": "foo/bar" 
}

这个是怎么实现的呢?Spring Boot 在启动时会注册一个 ErrorPageFilter,当 Servlet 发生异常时,该过滤器就会拦截处理,将异常根据不同的策略进行处理:当异常已经在处理的话直接处理,否则转发给对应的错误页面。有兴趣的可以去看下源码,逻辑不复杂,这里就不贴了。

另外当一个 Servlet 抛出一个异常时,处理异常的 Servlet 可以从 HttpServletRequest 里面得到几个属性,如下:
Spring Boot 统一异常处理的拦截指南
异常属性

我们可以从上面的几个属性中获取异常的详细信息。

默认错误页面

通常 Spring Boot 出现异常默认会跳转到 /error 进行处理,而 /error 的相关逻辑则是由 BasicErrorController 实现的。

@Controller 
@RequestMapping("${server.error.path:${error.path:/error}}") 
public class BasicErrorController extends AbstractErrorController { 
    // 返回错误页面 
  @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 
 public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {HttpStatus status = getStatus(request); 
  Map model = Collections 
    .unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML))); 
  response.setStatus(status.value()); 
  ModelAndView modelAndView = resolveErrorView(request, response, status, model); 
  return (modelAndView != null) ? modelAndView : new ModelAndView("error", model); 
 } 
    // 返回 json 
 @RequestMapping 
 public ResponseEntity> error(HttpServletRequest request) {HttpStatus status = getStatus(request); 
  if (status == HttpStatus.NO_CONTENT) {return new ResponseEntity(status); 
  } 
  Map body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL)); 
  return new ResponseEntity(body, status); 
 }   
// 其它省略 
} 

而对应的配置:

@Bean 
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT) 
public BasicErrorController basicErrorController(ErrorAttributes errorAttributes, 
      ObjectProvider errorViewResolvers) {return new BasicErrorController(errorAttributes, this.serverProperties.getError(), 
         errorViewResolvers.orderedStream().collect(Collectors.toList())); 
} 

所以我们只需要重新实现一个 ErrorController 并注入 Spring IoC 就可以替代默认的处理机制。而且我们可以很清晰的发现这个 BasicErrorController 不但是 ErrorController 的实现而且是一个控制器,如果我们让控制器的方法抛异常,肯定可以被自定义的统一异常处理。所以我对 BasicErrorController 进行了改造:

@Controller 
@RequestMapping("${server.error.path:${error.path:/error}}") 
public class ExceptionController extends AbstractErrorController {public ExceptionController(ErrorAttributes errorAttributes) {super(errorAttributes); 
    } 
 
 
    @Override 
    @Deprecated 
    public String getErrorPath() {return null;} 
 
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {throw new RuntimeException(getErrorMessage(request)); 
    } 
 
    @RequestMapping 
    public ResponseEntity> error(HttpServletRequest request) {throw new RuntimeException(getErrorMessage(request)); 
    } 
 
    private String getErrorMessage(HttpServletRequest request) {Object code = request.getAttribute("javax.servlet.error.status_code"); 
        Object exceptionType = request.getAttribute("javax.servlet.error.exception_type"); 
        Object message = request.getAttribute("javax.servlet.error.message"); 
        Object path = request.getAttribute("javax.servlet.error.request_uri"); 
        Object exception = request.getAttribute("javax.servlet.error.exception"); 
 
        return String.format("code: %s,exceptionType: %s,message: %s,path: %s,exception: %s", 
                code, exceptionType, message, path, exception); 
    } 
} 

直接抛异常,简单省力! 凡是这里捕捉的到的异常大部分还没有经过 Controller,我们通过 ExceptionController 中继也让这些异常被统一处理,保证整个应用的异常处理对外保持一个统一的门面。

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-07-25发表,共计3487字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中