默认规则
- 默认情况下,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 customize109it, add a View that resolves to error).
- 要对其进行自定义,添加View解析为error
- 要完全替换默认行为,可以实现 ErrorController 并注册该类型的Bean定义,或添加ErrorAttributes类型的组件以使用现有机制但替换其内容。
- error/下的4xx,5xx页面会被自动解析;
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 @Deprecatedpublic 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 reasonerrorAttributes.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】。(默认是一个白页)