Spring Boot 提供了两个默认的 HandlerExceptionResolver:
- DefaultErrorAttributes
- HandlerExceptionResolverComposite,包含 ExceptionHandlerExceptionResolver,ResponseStatusExceptionResolver,DefaultHandlerExceptionResolver
其中 DefaultErrorAttributes 是通过 ErrorMvcAutoConfiguration 注入容器中的,而 HandlerExceptionResolverComposite 是通过 WebMvcAutoConfiguration 注入的
ErrorMvcAutoConfigurationErrorMvcAutoConfiguration 注入 DefaultErrorAttributes、HandlerExceptionResolverComposite,其中 HandlerExceptionResolverComposite 是 3 个 HandlerExceptionResolver 的组合,由 WebMvcConfigurationSupport 硬编码注入
DefaultErrorAttributes如下代码是以 @Bean 的方式注入 DefaultErrorAttributes:
@Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() {
return new DefaultErrorAttributes();
}
其中条件注解 @ConditionalOnMissingBean 的属性 search = SearchStrategy.CURRENT 表示只在当前应用上下文搜索,不过通常情况下也不会定义多个应用上下文。
HandlerExceptionResolverCompositeHandlerExceptionResolverComposite 是由 3 个 HandlerExceptionResolver 的组合,由 WebMvcConfigurationSupport 硬编码注入。
为什么 DefaultErrorAttributes 和 HandlerExceptionResolverComposite 不是在同一个地方注入容器的?
因为 HandlerExceptionResolverComposite 是在 WebMvcConfigurationSupport 注入的,而 WebMvcConfigurationSupport 又与传统的 @EnableWebMvc 息息相关,Spring Boot 用 EnableWebMvcConfiguration 取代了 @EnableWebMvc,而 EnableWebMvcConfiguration 是 WebMvcAutoConfiguration 的静态内部类。同时,Spring Boot 又单独创建了 ErrorMvcAutoConfiguration,其中就配置有 DefaultErrorAttributes。大约可以理解为历史原因与 Spring Boot 设计。
BasicErrorController默认错误处理跳到到 /error
WhitelabelErrorViewConfiguration提供默认的白页支持
- 注入 id 为 error 的 View - 注入 BeanNameViewResolver,通过视图名称找到容器中的 View 对象
异常处理流程
从 DispatcherServlet 处理方法 doDispatch() 开始,HandlerAdapter 处理请求,如果处理过程中抛出 Exception 或者 Throwable 都会保存为 dispatchException,继续执行 processDispatchResult() 方法。
try {
// ...
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// ...
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
// ...
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
一般地,processDispatchResult() 将会调用 processHandlerException() 处理 Handler 抛出的异常,得到一个 mv。
if (exception instanceof ModelAndViewDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
// 一般情况
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(request, response, handler, exception);
errorView = (mv != null);
}
其中,processHandlerException() 将会遍历所有的 handlerExceptionResolvers 进行处理。
for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
if (exMv != null) {
break;
}
}
需要注意,与寻找 HandlerMapping,HandlerAdapter 不同,这里并不是找到最合适的解析器进行解析,而是可能有多个解析器解析,因为遍历的过程中会根据解析结果判断是否继续解析,如果返回的结果是 null,则继续解析。例如,DefaultErrorAttributes 也是一个 HandlerExceptionResolver,但它处理的时候只是进行错误属性的存储,并不进行实际的解析,所以它返回 null,让下面的解析器继续解析。而且,DefaultErrorAttributes 进行了 @Order(Ordered.HIGHEST_PRECEDENCE) 注解,确保其能优先被处理。
如果没有任何 HandlerExceptionResolver 解析器能够解析,会被转发到 /error 请求,之后会被 BasicErrorController 处理。
如果选择的是 errorHtml 方法,那么会调用 resolveErrorView() 方法,遍历 errorViewResolvers 让错误视图解析器进行解析。一般地,会在 ErrorMvcAutoConfiguration 注入一个 DefaultErrorViewResolver,但是这个配置已经过时了。
如果你希望错误暴露到 Tomcat 等 Servlet 容器,你需要实现 ErrorController,并通过 handler 将异常继续抛出。但是这并没有什么用



