SpringBoot-27 异常自动配置ErrorMvcAutoConfiguration

1、默认规则

  • 默认情况下,Spring Boot提供/error处理所有错误的映射
  • 对于机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据
By default, Spring Boot provides an /error mapping that handles all errors in a sensible way, and it
is registered as a “global” error page in the servlet container. For machine clients, it produces a
JSON response with details of the error, the HTTP status, and the exception message. For browser
clients, there is a “whitelabel” error view that renders the same data in HTML format (to customize
109
it, add a View that resolves to error).

 

  • 要对其进行自定义,添加View解析为error
  • 要完全替换默认行为,可以实现 ErrorController 并注册该类型的Bean定义,或添加ErrorAttributes类型的组件以使用现有机制但替换其内容。
  • error/下的4xx,5xx页面会被自动解析;
    • image.png

 

 


 2、定制错误处理逻辑

  • 自定义错误页
    • error/404.html   error/5xx.html;有精确的错误状态码页面就匹配精确,没有就找 4xx.html;如果都没有就触发白页
  • @ControllerAdvice+@ExceptionHandler处理全局异常;底层是 ExceptionHandlerExceptionResolver 支持的
  • @ResponseStatus+自定义异常 ;底层是 ResponseStatusExceptionResolver ,把responsestatus注解的信息底层调用 response.sendError(statusCode, resolvedReason);tomcat发送的/error
  • Spring底层的异常,如 参数类型转换异常;DefaultHandlerExceptionResolver 处理框架底层的异常。
    • response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
    • image.png
  • 自定义实现 HandlerExceptionResolver 处理异常;可以作为默认的全局异常处理规则
    • image.png
  • ErrorViewResolver  实现自定义处理异常;
    • response.sendError 。error请求就会转给controller
    • 你的异常没有任何人能处理。tomcat底层 response.sendError。error请求就会转给controller
    • basicErrorController 要去的页面地址是 ErrorViewResolver  ;

代码,注解@ControllerAdvice+@ExceptionHandler,这样可以被ExceptionHandlerExceptionResolver解析

/**
 * 处理全局web contoller异常
 */
@Slf4j
@ControllerAdvice
public class GlobleExceptionHandler {
    @ExceptionHandler({ArithmeticException.class,NullPointerException.class})
    public String handleArithmeticException(Exception e){
        log.error("异常是:{}"+e);

        return "login";//返回视图地址

    }
}

代码:@ResponseStatus+自定义异常 

@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "当前用户太多!")
public class UserTooManyException extends RuntimeException {
    public UserTooManyException(){

    }
    public UserTooManyException(String message){
        super(message);
    }

}

代码,自定义异常解析器,并设置优先级(PS:如果不设置优先级,可能被其他解析器提前处理)

@Order(value = Ordered.HIGHEST_PRECEDENCE)//设置最高优先级
@Component
public class CustomerizeHandlerExceptionResolver implements HandlerExceptionResolver
{
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception ex) {

        try {
            response.sendError(511,"自定义错误!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ModelAndView();
    }
}

 


 

3、异常处理自动配置原理

3.1 ErrorMvcAutoConfiguration  自动配置异常处理规则

3.1.1 配置属性:@EnableConfigurationProperties({ ServerProperties.class, WebMvcProperties.class })

  • ServerProperties--->errorproperties--->private String path = "/error";

3.1.2 容器组件类型:DefaultErrorAttributes -> id:errorAttributes

    @Bean
    @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
    public DefaultErrorAttributes errorAttributes() {
        return new DefaultErrorAttributes();
    }
  • public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver, Ordered
  • DefaultErrorAttributes:定义错误页面中可以包含哪些数据
  •     public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
            Map<String, Object> errorAttributes = getErrorAttributes(webRequest, options.isIncluded(Include.STACK_TRACE));
            if (Boolean.TRUE.equals(this.includeException)) {
                options = options.including(Include.EXCEPTION);
            }
            if (!options.isIncluded(Include.EXCEPTION)) {
                errorAttributes.remove("exception");
            }
            if (!options.isIncluded(Include.STACK_TRACE)) {
                errorAttributes.remove("trace");
            }
            if (!options.isIncluded(Include.MESSAGE) && errorAttributes.get("message") != null) {
                errorAttributes.put("message", "");
            }
            if (!options.isIncluded(Include.BINDING_ERRORS)) {
                errorAttributes.remove("errors");
            }
            return errorAttributes;
        }
  • @Override
        @Deprecated
        public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
            Map<String, Object> errorAttributes = new LinkedHashMap<>();
            errorAttributes.put("timestamp", new Date());
            addStatus(errorAttributes, webRequest);
            addErrorDetails(errorAttributes, webRequest, includeStackTrace);
            addPath(errorAttributes, webRequest);
            return errorAttributes;
        }
    
        private void addStatus(Map<String, Object> errorAttributes, RequestAttributes requestAttributes) {
            Integer status = getAttribute(requestAttributes, RequestDispatcher.ERROR_STATUS_CODE);
            if (status == null) {
                errorAttributes.put("status", 999);
                errorAttributes.put("error", "None");
                return;
            }
            errorAttributes.put("status", status);
            try {
                errorAttributes.put("error", HttpStatus.valueOf(status).getReasonPhrase());
            }
            catch (Exception ex) {
                // Unable to obtain a reason
                errorAttributes.put("error", "Http Status " + status);
            }
        }

3.1.3容器中的组件:类型:BasicErrorController --> id:basicErrorController(json+白页 适配响应)

@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController{}
  • 是一个Controller
  • 类上注解:@RequestMapping("${server.error.path:${error.path:/error}}")
  • 处理默认 /error 路径的请求;
  • 页面响应 new ModelAndView("error", model);
  • 容器中有组件 View->id是error;(响应默认错误页)
  • 容器中放组件 BeanNameViewResolver(视图解析器);按照返回的视图名作为组件的id去容器中找View对象。

3.1.4 容器中的组件:类型:DefaultErrorViewResolver -> id:conventionErrorViewResolver

  • 如果发生错误,会以HTTP的状态码 作为视图页地址(viewName),找到真正的页面
  • error/404、5xx.html

 如果想要返回页面;就会找error视图【StaticView】。(默认是一个白页)

 

 

 


 

 4、异常处理步骤流程

1、执行目标方法,目标方法运行期间有任何异常都会被catch、而且标志当前请求结束(requstCompletion?此处记不清);并且用 dispatchException

2、进入视图解析流程(页面渲染?)

processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);

3、mv = processHandlerException;处理handler发生的异常,处理完成返回ModelAndView;

  • 1、遍历所有的 handlerExceptionResolvers,看谁能处理当前异常【HandlerExceptionResolver处理器异常解析器
  • image.png
  • 2、系统默认的  异常解析器;
  • image.png
    • 1、DefaultErrorAttributes先来处理异常。把异常信息保存到rrequest域,并且返回null;
    • 2、默认没有任何人能处理异常,所以异常会被抛出
      • 1、如果没有任何人能处理最终底层就会发送 /error 请求。会被底层的BasicErrorController处理
      • 2、解析错误视图;遍历所有的  ErrorViewResolver  看谁能解析。
      • image.png
      • 3、默认的 DefaultErrorViewResolver ,作用是把响应状态码作为错误页的地址,error/500.html
      • 4、模板引擎最终响应这个页面 error/500.html 

 

 

 

 

 

posted @ 2021-03-27 22:51  少时也曾爱白衣  阅读(2446)  评论(0)    收藏  举报