1. 程式人生 > >Spring Boot異常處理詳解

Spring Boot異常處理詳解

Spring MVC異常處理詳解中,介紹了Spring MVC的異常處理體系,本文將講解在此基礎上Spring Boot為我們做了哪些工作。下圖列出了Spring Boot中跟MVC異常處理相關的類。

Spring Boot在啟動過程中會根據當前環境進行AutoConfiguration,其中跟MVC錯誤處理相關的配置內容,在ErrorMvcAutoConfiguration這個類中。以下會分塊介紹這個類裡面的配置。

在Servlet容器中添加了一個預設的錯誤頁面

因為ErrorMvcAutoConfiguration類實現了EmbeddedServletContainerCustomizer介面,所以可以通過override customize方法來定製Servlet容器。以下程式碼摘自ErrorMvcAutoConfiguration:

@Value("${error.path:/error}")
private String errorPath = "/error";

@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
    container.addErrorPages(new ErrorPage(this.properties.getServletPrefix()
        + this.errorPath));
}

可以看到ErrorMvcAutoConfiguration在容器中,添加了一個錯誤頁面/error。因為這項配置的存在,如果Spring MVC在處理過程丟擲異常到Servlet容器,容器會定向到這個錯誤頁面/error。

那麼我們有什麼可以配置的呢?

  1. 我們可以配置錯誤頁面的url,/error是預設值,我們可以再application.properties中通過設定error.path的值來配置該頁面的url;
  2. 我們可以提供一個自定義的EmbeddedServletContainerCustomizer,新增更多的錯誤頁面,比如對不同的http status code,使用不同的錯誤處理頁面。就像下面這段程式碼一樣:
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return
new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404")); container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500")); } }; }

定義了ErrorAttributes介面,並預設配置了一個DefaultErrorAttributes Bean

以下程式碼摘自ErrorMvcAutoConfiguration:

@Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes();
}

以下程式碼摘自DefaultErrorAttributes, ErrorAttributes, HandlerExceptionResolver:

@Order(Ordered.HIGHEST_PRECEDENCE)
public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver,
    Ordered {
    //篇幅原因,忽略類的實現程式碼。
}

public interface ErrorAttributes {
    public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
        boolean includeStackTrace);
    public Throwable getError(RequestAttributes requestAttributes);
}

public interface HandlerExceptionResolver {
    ModelAndView resolveException(
        HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);
}

這個DefaultErrorAttributes有什麼用呢?主要有兩個作用:

  1. 實現了ErrorAttributes介面,具備提供Error Attributes的能力,當處理/error錯誤頁面時,需要從該bean中讀取錯誤資訊以供返回;
  2. 實現了HandlerExceptionResolver介面並具有最高優先順序,即DispatcherServlet在doDispatch過程中有異常丟擲時,先由DefaultErrorAttributes處理。從下面程式碼中可以發現,DefaultErrorAttributes在處理過程中,是講ErrorAttributes儲存到了request中。事實上,這是DefaultErrorAttributes能夠在後面返回Error Attributes的原因,實現HandlerExceptionResolver介面,是DefaultErrorAttributes實現ErrorAttributes介面的手段。
@Override
public ModelAndView resolveException(HttpServletRequest request,
    HttpServletResponse response, Object handler, Exception ex) {
    storeErrorAttributes(request, ex);
    return null;
}

我們有什麼可以配置的呢?
1、我們可以繼承DefaultErrorAttributes,修改Error Attributes,比如下面這段程式碼,去掉了預設存在的error和exception這兩個欄位,以隱藏敏感資訊。

@Bean
public DefaultErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes() {
        @Override
        public Map<String, Object> getErrorAttributes (RequestAttributes requestAttributes,
        boolean includeStackTrace){
            Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
            errorAttributes.remove("error");
            errorAttributes.remove("exception");
            return errorAttributes;
        }
    };
}
  1. 我們可以自己實現ErrorAttributes介面,實現自己的Error Attributes方案, 只要配置一個型別為ErrorAttributes的bean,預設的DefaultErrorAttributes就不會被配置。

提供並配置了ErrorController和ErrorView

ErrorController和ErrorView提供了對錯誤頁面/error的支援。ErrorMvcAutoConfiguration預設配置了BasicErrorController和WhiteLabelErrorView,以下程式碼摘自ErrorMvcAutoConfiguration:

@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
    return new BasicErrorController(errorAttributes);
}

@Configuration
@ConditionalOnProperty(prefix = "error.whitelabel", name = "enabled", matchIfMissing = true)
@Conditional(ErrorTemplateMissingCondition.class)
protected static class WhitelabelErrorViewConfiguration {
    private final SpelView defaultErrorView = new SpelView(
            "<html><body><h1>Whitelabel Error Page</h1>"
                    + "<p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p>"
                    + "<div id='created'>${timestamp}</div>"
                    + "<div>There was an unexpected error (type=${error}, status=${status}).</div>"
                    + "<div>${message}</div></body></html>");

    @Bean(name = "error")
    @ConditionalOnMissingBean(name = "error")
    public View defaultErrorView() {
        return this.defaultErrorView;
    }

    // If the user adds @EnableWebMvc then the bean name view resolver from
    // WebMvcAutoConfiguration disappears, so add it back in to avoid disappointment.
    @Bean
    @ConditionalOnMissingBean(BeanNameViewResolver.class)
    public BeanNameViewResolver beanNameViewResolver() {
        BeanNameViewResolver resolver = new BeanNameViewResolver();
        resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 10);
        return resolver;
    }
}

ErrorController根據Accept頭的內容,輸出不同格式的錯誤響應。比如針對瀏覽器的請求生成html頁面,針對其它請求生成json格式的返回。程式碼如下:

@RequestMapping(value = "${error.path:/error}", produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request) {
    return new ModelAndView("error", getErrorAttributes(request, false));
}

@RequestMapping(value = "${error.path:/error}")
@ResponseBody
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
    Map<String, Object> body = getErrorAttributes(request, getTraceParameter(request));
    HttpStatus status = getStatus(request);
    return new ResponseEntity<Map<String, Object>>(body, status);
}

WhitelabelErrorView則提供了一個預設的白板錯誤頁面。

我們有什麼可以配置的呢?

  1. 我們可以提供自己的名字為error的view,以替換掉預設的白板頁面,提供自己想要的樣式。
  2. 我們可以繼承BasicErrorController或者乾脆自己實現ErrorController介面,用來響應/error這個錯誤頁面請求,可以提供更多型別的錯誤格式等。

總結

Spring Boot提供了預設的統一錯誤頁面,這是Spring MVC沒有提供的。在理解了Spring Boot提供的錯誤處理相關內容之後,我們可以方便的定義自己的錯誤返回的格式和內容。不過,如果要實現統一的REST API介面的出錯響應,就如這篇文章裡的這樣,還是要做不少工作的。