温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Boot统一异常处理能拦截所有的异常吗

发布时间:2021-07-02 16:00:03 来源:亿速云 阅读:256 作者:chen 栏目:开发技术

本篇内容介绍了“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<String, Object> 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<Map<String, Object>> error(HttpServletRequest request) {   HttpStatus status = getStatus(request);   if (status == HttpStatus.NO_CONTENT) {    return new ResponseEntity<>(status);   }   Map<String, Object> 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<ErrorViewResolver> 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<Map<String, Object>> 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中继也让这些异常被统一处理,保证整个应用的异常处理对外保持一个统一的门面。

“Spring Boot统一异常处理能拦截所有的异常吗”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI