SpringBoot 2.1.4 錯誤處理機制
springboot的自動配置中幫我們配置了相關的錯誤處理組件,例如訪問一個不存在的頁面,就會出現下面的錯誤頁面,上面也會顯示相應的信息
在Postman軟件中模擬移動端訪問,會獲取如下響應的json數據:
可以發現springboot的錯誤處理機制很好的適應了不同客戶端訪問,瀏覽器返回頁面,移動端返回json,那這背后springboot是如何處理的,顯示的頁面我想自己設計,或者返回的這些信息我們自己能夠定制嗎?
SpringBoot錯誤機制原理
springboot版本:2.1.4.RELEASE
1、默認錯誤頁面生成機制
當我們在訪問一個不存在的路徑時,會出現上面的錯誤頁面,這個頁面不是我們自己創建的,而是由springboot幫我們生成的,那下面我們首先弄清楚這個默認的錯誤頁面(Whitelabel Error Page)是怎么生成的。
1.1 springboot關于error的自動配置
在package org.springframework.boot.autoconfigure.web.servlet.error包下有如下的類:
- BasicErrorController、AbstractErrorController:錯誤請求控制器
- DefaultErrorViewResolver:錯誤視圖解析器
- ErrorMvcAutoConfiguration:error的自動配置類
ErrorMvcAutoConfiguration
在這個配置類中注冊了一些組件:
@Bean @ConditionalOnMissingBean( value = {ErrorAttributes.class}, search = SearchStrategy.CURRENT ) // 關于error錯誤信息的相關類 public DefaultErrorAttributes errorAttributes() { return new DefaultErrorAttributes(this.serverProperties.getError().isIncludeException()); } @Bean @ConditionalOnMissingBean( value = {ErrorController.class}, search = SearchStrategy.CURRENT ) // 處理錯誤請求的控制器 public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) { return new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers); } @Bean // 錯誤頁面定制器 public ErrorMvcAutoConfiguration.ErrorPageCustomizer errorPageCustomizer() { return new ErrorMvcAutoConfiguration.ErrorPageCustomizer(this.serverProperties, this.dispatcherServletPath); }
第一步:ErrorPageCustomizer
private static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered { private final ServerProperties properties; private final DispatcherServletPath dispatcherServletPath; protected ErrorPageCustomizer(ServerProperties properties, DispatcherServletPath dispatcherServletPath) { this.properties = properties; this.dispatcherServletPath = dispatcherServletPath; } public void registerErrorPages(ErrorPageRegistry errorPageRegistry) { // getPath()獲取到一個路徑“/error” ErrorPage errorPage = new ErrorPage(this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath())); // 關鍵點:這里講將/error的errorPage注冊到了servlet,在發生異常時就會轉發到/error errorPageRegistry.addErrorPages(new ErrorPage[]{errorPage}); } public int getOrder() { return 0; } }
注意上面的注釋,這里是為什么發生錯誤就會發起/error,很多博客都未說明,當然這里沒有討論其內部原理。
第二步:BasicErrorController
在錯誤發生后,發起 “/error” 請求,那這個 “/error” 就會由上面已經注冊的BasicErrorController 接收處理。
@Controller // 表明是個控制器 @RequestMapping({"${server.error.path:${error.path:/error}}"}) // 映射的路徑:/error public class BasicErrorController extends AbstractErrorController { private final ErrorProperties errorProperties; public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) { this(errorAttributes, errorProperties, Collections.emptyList()); } public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) { super(errorAttributes, errorViewResolvers); Assert.notNull(errorProperties, "ErrorProperties must not be null"); this.errorProperties = errorProperties; } public String getErrorPath() { return this.errorProperties.getPath(); } // 處理瀏覽器的請求 @RequestMapping( produces = {"text/html"} ) public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { HttpStatus status = this.getStatus(request); Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML))); response.setStatus(status.value()); ModelAndView modelAndView = this.resolveErrorView(request, response, status, model); return modelAndView != null ? modelAndView : new ModelAndView("error", model); } // 處理移動端的請求 @RequestMapping public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { Map<String, Object> body = this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL)); HttpStatus status = this.getStatus(request); return new ResponseEntity(body, status); } protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) { IncludeStacktrace include = this.getErrorProperties().getIncludeStacktrace(); if (include == IncludeStacktrace.ALWAYS) { return true; } else { return include == IncludeStacktrace.ON_TRACE_PARAM ? this.getTraceParameter(request) : false; } } protected ErrorProperties getErrorProperties() { return this.errorProperties; } }
這里可以解決一個疑惑,springboot怎么區分是瀏覽器還是移動端的,主要看這個方法的注解 produces={“text/html”} ,表示響應的數據是以html形式返回,這樣當瀏覽器訪問時就會調用這個方法
@RequestMapping(produces = {"text/html"}) public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response){ ...
客戶端訪問時就會調用下面的error方法。
@RequestMapping public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
下面再來具體分析默認錯誤頁面如何生成,還是來看到errorHTML方法:
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { // 獲取錯誤狀態碼,封裝到HttpStatus里面 HttpStatus status = this.getStatus(request); // 獲取錯誤信息,以map形式返回,這個后面我們具體來看,到底我們能獲取到哪些數據 Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML))); // 設置響應體中狀態碼 response.setStatus(status.value()); // 關鍵點:這里就是在創建視圖對象 ModelAndView modelAndView = this.resolveErrorView(request, response, status, model); return modelAndView != null ? modelAndView : new ModelAndView("error", model); }
下面來看這個resolveErrorView方法,這個方法是父類AbstractErrorController 中的:
protected ModelAndView resolveErrorView(HttpServletRequest request, HttpServletResponse response, HttpStatus status, Map<String, Object> model) { // errorViewResolvers是一個list,存放ErrorViewResolver對象 Iterator var5 = this.errorViewResolvers.iterator(); ModelAndView modelAndView; // 遍歷集合 do { if (!var5.hasNext()) { return null; } ErrorViewResolver resolver = (ErrorViewResolver)var5.next(); // 關鍵點:解析器對象進行視圖解析 modelAndView = resolver.resolveErrorView(request, status, model); } while(modelAndView == null); return modelAndView; }
這里的resolveErrorView方法屬于DefaultErrorViewResolver:
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) { // 調用下面的方法解析視圖,傳入參數為錯誤狀態碼,錯誤信息的map ModelAndView modelAndView = this.resolve(String.valueOf(status.value()), model); if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) { modelAndView = this.resolve((String)SERIES_VIEWS.get(status.series()), model); } return modelAndView; } private ModelAndView resolve(String viewName, Map<String, Object> model) { // 定義視圖名,這里我們可以確定視圖名:error/錯誤碼,例如:error/404, String errorViewName = "error/" + viewName; // 這里結合上面的errorViewName,其實就是在template目錄下的error目錄進行查找 // 我們默認情況下是沒有error目錄,這里的provide最終值為null,代碼較多就不一一展示,有興趣的可以跟下去 TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, this.applicationContext); // 根據判定,這里會接著調用下面的resolveResource方法 return provider != null ? new ModelAndView(errorViewName, model) : this.resolveResource(errorViewName, model); } private ModelAndView resolveResource(String viewName, Map<String, Object> model) { // getStaticLocations()獲取的是靜態資源路徑:"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/" String[] var3 = this.resourceProperties.getStaticLocations(); int var4 = var3.length; // 遍歷上面的4個靜態資源路徑 for(int var5 = 0; var5 < var4; ++var5) { String location = var3[var5]; try { Resource resource = this.applicationContext.getResource(location); // 創建resource對象,例如error/404.html resource = resource.createRelative(viewName + ".html"); // 查找在對應靜態資源目錄下是否有上面的這個資源對象,有就創建視圖對象 if (resource.exists()) { return new ModelAndView(new DefaultErrorViewResolver.HtmlResourceView(resource), model); } } catch (Exception var8) { ; } } // 都沒找到就返回null,默認情況下是不存在error目錄的,所以這里最終返回null return null; }
當resolveResource方法執行完返回null,resolve方法也就返回null,在回到resolveErrorView
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) { // 調用下面的方法解析視圖,傳入參數為錯誤狀態碼,錯誤信息的map ModelAndView modelAndView = this.resolve(String.valueOf(status.value()), model); // 上面分析得到modelAndView的值為null,下面的if中SERIES_VIEWS.containsKey(status.series())是在判斷錯誤碼的首位是否為1,2,3,4,5,這個大家下去可以跟一下 if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) { modelAndView = this.resolve((String)SERIES_VIEWS.get(status.series()), model); } // if里面的resolve方法分析跟上面一樣,默認情況下是沒有4xx.html/5xx.html頁面文件的,所以最終這里返回null return modelAndView; }
這個resolveErrorView方法執行完后,我們就可以回到最開始處理 “/error” 請求的errorHtml方法了
@RequestMapping(produces = {"text/html"} ) public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { HttpStatus status = this.getStatus(request); Map<String, Object> model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.TEXT_HTML))); response.setStatus(status.value()); ModelAndView modelAndView = this.resolveErrorView(request, response, status, model); // modelAnView根據上面的分析其值為null return modelAndView != null ? modelAndView : new ModelAndView("error", model); }
當modelAndView為null時,將會執行'new ModelAndView(“error”, model),那這個“error”又是什么呢?看下面WhitelabelErrorViewConfiguration 里面有個組件其 name就是error,這個組件是StaticView,就是一個View,里面的視圖渲染方法render中的內容就是最開始我們看到的那個錯誤頁面的內容。
@Conditional({ErrorMvcAutoConfiguration.ErrorTemplateMissingCondition.class}) protected static class WhitelabelErrorViewConfiguration { private final ErrorMvcAutoConfiguration.StaticView defaultErrorView = new ErrorMvcAutoConfiguration.StaticView(); protected WhitelabelErrorViewConfiguration() { } @Bean(name = {"error"}) @ConditionalOnMissingBean(name = {"error"}) public View defaultErrorView() { return this.defaultErrorView; } ... } private static class StaticView implements View { private static final Log logger = LogFactory.getLog(ErrorMvcAutoConfiguration.StaticView.class); private StaticView() { } public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { if (response.isCommitted()) { String message = this.getMessage(model); logger.error(message); } else { StringBuilder builder = new StringBuilder(); Date timestamp = (Date)model.get("timestamp"); Object message = model.get("message"); Object trace = model.get("trace"); if (response.getContentType() == null) { response.setContentType(this.getContentType()); } builder.append("<html><body><h1>Whitelabel Error Page</h1>").append("<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>").append("<div id='created'>").append(timestamp).append("</div>").append("<div>There was an unexpected error (type=").append(this.htmlEscape(model.get("error"))).append(", status=").append(this.htmlEscape(model.get("status"))).append(").</div>"); if (message != null) { builder.append("<div>").append(this.htmlEscape(message)).append("</div>"); } if (trace != null) { builder.append("<div style='white-space:pre-wrap;'>").append(this.htmlEscape(trace)).append("</div>"); } builder.append("</body></html>"); response.getWriter().append(builder.toString()); } } private String htmlEscape(Object input) { return input != null ? HtmlUtils.htmlEscape(input.toString()) : null; } private String getMessage(Map<String, ?> model) { Object path = model.get("path"); String message = "Cannot render error page for request [" + path + "]"; if (model.get("message") != null) { message = message + " and exception [" + model.get("message") + "]"; } message = message + " as the response has already been committed."; message = message + " As a result, the response may have the wrong status code."; return message; } public String getContentType() { return "text/html"; } }
所以,整個大致的過程到此結束了,默認情況下錯誤請求處理完成后就返回的這個StaticView定義的頁面,下圖做個基本的梳理。后續再來做自定義錯誤頁面、自定義錯誤數據的原理分析。
SpringBoot 2.1.3 錯誤處理機制
引用的問題做個標記
以前的引用好像在新版本中無法引用了
錯誤處理機制
其他的程序的類的聲明直接用IDEA的提示來用就可以了。
如果還是有錯誤的話,就進入到lib中看看引用的類的方法就可以了
import org.springframework.boot.autoconfigration.web.DefaultErrorAttributes;//這是以前的 import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;//這是現在的
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_34975710/article/details/89892048