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頁面會被自動解析;
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());

- 自定義實現 HandlerExceptionResolver 處理異常;可以作為默認的全局異常處理規則
- 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處理器異常解析器】

- 2、系統默認的 異常解析器;

- 1、DefaultErrorAttributes先來處理異常。把異常信息保存到rrequest域,並且返回null;
- 2、默認沒有任何人能處理異常,所以異常會被拋出
- 1、如果沒有任何人能處理最終底層就會發送 /error 請求。會被底層的BasicErrorController處理
- 2、解析錯誤視圖;遍歷所有的 ErrorViewResolver 看誰能解析。

- 3、默認的 DefaultErrorViewResolver ,作用是把響應狀態碼作為錯誤頁的地址,error/500.html
- 4、模板引擎最終響應這個頁面 error/500.html


