Spring Boot異常處理詳解

jopen 9年前發布 | 166K 次閱讀 Spring JEE框架 Spring Boot


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

Spring Boot異常處理詳解

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接口的出錯響應,就如 這篇文章 里的這樣,還是要做不少工作的。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!