欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Springboot自定義異常處理

 更新時(shí)間:2017年04月06日 16:06:06   作者:whatlookingfor  
本篇文章主要介紹了詳解Springboot自定義異常處理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

背景

Springboot 默認(rèn)把異常的處理集中到一個(gè)ModelAndView中了,但項(xiàng)目的實(shí)際過程中,這樣做,并不能滿足我們的要求。具體的自定義異常的處理,參看以下

具體實(shí)現(xiàn)

如果仔細(xì)看完spring boot的異常處理詳解,并且研究過源碼后,我覺得具體的實(shí)現(xiàn)可以不用看了。。。

重寫定義錯(cuò)誤頁(yè)面的url,默認(rèn)只有一個(gè)/error

@Bean
  public EmbeddedServletContainerCustomizer containerCustomizer(){
    return new EmbeddedServletContainerCustomizer(){
      @Override
      public void customize(ConfigurableEmbeddedServletContainer container) {
        container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/error/404"));
        container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500"));
        container.addErrorPages(new ErrorPage(java.lang.Throwable.class,"/error/500"));
      }
    };
  }

重寫通過實(shí)現(xiàn)ErrorController,重寫B(tài)asicErrorController的功能實(shí)現(xiàn)

/**
 * 重寫B(tài)asicErrorController,主要負(fù)責(zé)系統(tǒng)的異常頁(yè)面的處理以及錯(cuò)誤信息的顯示
 * @see org.springframework.boot.autoconfigure.web.BasicErrorController
 * @see org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
 *
 * @author Jonathan
 * @version 2016/5/31 11:22
 * @since JDK 7.0+
 */
@Controller
@RequestMapping(value = "error")
@EnableConfigurationProperties({ServerProperties.class})
public class ExceptionController implements ErrorController {

  private ErrorAttributes errorAttributes;

  @Autowired
  private ServerProperties serverProperties;


  /**
   * 初始化ExceptionController
   * @param errorAttributes
   */
  @Autowired
  public ExceptionController(ErrorAttributes errorAttributes) {
    Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
    this.errorAttributes = errorAttributes;
  }


  /**
   * 定義404的ModelAndView
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(produces = "text/html",value = "404")
  public ModelAndView errorHtml404(HttpServletRequest request,
                 HttpServletResponse response) {
    response.setStatus(getStatus(request).value());
    Map<String, Object> model = getErrorAttributes(request,
        isIncludeStackTrace(request, MediaType.TEXT_HTML));
    return new ModelAndView("error/404", model);
  }

  /**
   * 定義404的JSON數(shù)據(jù)
   * @param request
   * @return
   */
  @RequestMapping(value = "404")
  @ResponseBody
  public ResponseEntity<Map<String, Object>> error404(HttpServletRequest request) {
    Map<String, Object> body = getErrorAttributes(request,
        isIncludeStackTrace(request, MediaType.TEXT_HTML));
    HttpStatus status = getStatus(request);
    return new ResponseEntity<Map<String, Object>>(body, status);
  }

  /**
   * 定義500的ModelAndView
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(produces = "text/html",value = "500")
  public ModelAndView errorHtml500(HttpServletRequest request,
                 HttpServletResponse response) {
    response.setStatus(getStatus(request).value());
    Map<String, Object> model = getErrorAttributes(request,
        isIncludeStackTrace(request, MediaType.TEXT_HTML));
    return new ModelAndView("error/500", model);
  }


  /**
   * 定義500的錯(cuò)誤JSON信息
   * @param request
   * @return
   */
  @RequestMapping(value = "500")
  @ResponseBody
  public ResponseEntity<Map<String, Object>> error500(HttpServletRequest request) {
    Map<String, Object> body = getErrorAttributes(request,
        isIncludeStackTrace(request, MediaType.TEXT_HTML));
    HttpStatus status = getStatus(request);
    return new ResponseEntity<Map<String, Object>>(body, status);
  }


  /**
   * Determine if the stacktrace attribute should be included.
   * @param request the source request
   * @param produces the media type produced (or {@code MediaType.ALL})
   * @return if the stacktrace attribute should be included
   */
  protected boolean isIncludeStackTrace(HttpServletRequest request,
                     MediaType produces) {
    ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
    if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
      return true;
    }
    if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
      return getTraceParameter(request);
    }
    return false;
  }


  /**
   * 獲取錯(cuò)誤的信息
   * @param request
   * @param includeStackTrace
   * @return
   */
  private Map<String, Object> getErrorAttributes(HttpServletRequest request,
                          boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    return this.errorAttributes.getErrorAttributes(requestAttributes,
        includeStackTrace);
  }

  /**
   * 是否包含trace
   * @param request
   * @return
   */
  private boolean getTraceParameter(HttpServletRequest request) {
    String parameter = request.getParameter("trace");
    if (parameter == null) {
      return false;
    }
    return !"false".equals(parameter.toLowerCase());
  }

  /**
   * 獲取錯(cuò)誤編碼
   * @param request
   * @return
   */
  private HttpStatus getStatus(HttpServletRequest request) {
    Integer statusCode = (Integer) request
        .getAttribute("javax.servlet.error.status_code");
    if (statusCode == null) {
      return HttpStatus.INTERNAL_SERVER_ERROR;
    }
    try {
      return HttpStatus.valueOf(statusCode);
    }
    catch (Exception ex) {
      return HttpStatus.INTERNAL_SERVER_ERROR;
    }
  }

  /**
   * 實(shí)現(xiàn)錯(cuò)誤路徑,暫時(shí)無(wú)用
   * @see ExceptionMvcAutoConfiguration#containerCustomizer()
   * @return
   */
  @Override
  public String getErrorPath() {
    return "";
  }

}

總結(jié)

第一步,通過定義containerCustomizer,重寫定義了異常處理對(duì)應(yīng)的視圖。目前定義了404和500,可以繼續(xù)擴(kuò)展。

第二步,重寫B(tài)asicErrorController,當(dāng)然可以直接定義一個(gè)普通的controller類,直接實(shí)現(xiàn)第一步定義的視圖的方法。重寫的目的是重用ErrorAttributes。這樣在頁(yè)面,直接可以獲取到status,message,exception,trace等內(nèi)容。

如果僅僅是把異常處理的視圖作為靜態(tài)頁(yè)面,不需要看到異常信息內(nèi)容的話,直接第一步后,再定義error/404,error/500等靜態(tài)視圖即可。

ErrorController根據(jù)Accept頭的內(nèi)容,輸出不同格式的錯(cuò)誤響應(yīng)。比如針對(duì)瀏覽器的請(qǐng)求生成html頁(yè)面,針對(duì)其它請(qǐng)求生成json格式的返回

以上兩步的操作,比網(wǎng)上流傳的更能實(shí)現(xiàn)自定義化。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)

    SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)

    這篇文章主要介紹了SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備),詳細(xì)的介紹延遲隊(duì)列的使用場(chǎng)景及其如何使用,需要的小伙伴可以一起來了解一下
    2019-12-12
  • MyBatis在DAO層定義接口返回類型泛型無(wú)效的解決

    MyBatis在DAO層定義接口返回類型泛型無(wú)效的解決

    這篇文章主要介紹了MyBatis在DAO層定義接口返回類型泛型無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)

    深入學(xué)習(xí)Java同步機(jī)制中的底層實(shí)現(xiàn)

    在多線程編程中我們會(huì)遇到很多需要使用線程同步機(jī)制去解決的并發(fā)問題,這些同步機(jī)制是如何實(shí)現(xiàn)的呢?下面和小編來一起學(xué)習(xí)吧
    2019-05-05
  • javaweb前端向后端傳值的幾種方式總結(jié)(附代碼)

    javaweb前端向后端傳值的幾種方式總結(jié)(附代碼)

    javaweb是java開發(fā)中的一個(gè)方向,下面這篇文章主要給大家介紹了關(guān)于javaweb前端向后端傳值的幾種方式的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • Java中資源加載的方法及Spring的ResourceLoader應(yīng)用小結(jié)

    Java中資源加載的方法及Spring的ResourceLoader應(yīng)用小結(jié)

    在Java開發(fā)中,資源加載是一個(gè)基礎(chǔ)而重要的操作,這篇文章主要介紹了深入理解Java中資源加載的方法及Spring的ResourceLoader應(yīng)用,本文通過實(shí)例代碼演示了通過ClassLoader和Class獲取資源的內(nèi)容,以及使用Spring的ResourceLoader加載多個(gè)資源的過程,需要的朋友可以參考下
    2024-01-01
  • SpringBoot集成極光推送的實(shí)現(xiàn)代碼

    SpringBoot集成極光推送的實(shí)現(xiàn)代碼

    工作中經(jīng)常會(huì)遇到服務(wù)器向App推送消息的需求,一般企業(yè)中選擇用極光推送的比較多,本文就介紹了SpringBoot集成極光推送的實(shí)現(xiàn)代碼,感興趣的可以了解一下
    2023-08-08
  • java選擇框、單選框和單選按鈕

    java選擇框、單選框和單選按鈕

    本文給大家介紹的是java中選擇框、單選框和單選按鈕的操作方法,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。
    2015-06-06
  • Java web的讀取Excel簡(jiǎn)單實(shí)例代碼

    Java web的讀取Excel簡(jiǎn)單實(shí)例代碼

    下面小編就為大家?guī)硪黄狫ava web的讀取Excel簡(jiǎn)單實(shí)例代碼。小編覺挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • java虛擬機(jī)是做什么用的

    java虛擬機(jī)是做什么用的

    在本篇文章里小編給大家整理的是一篇關(guān)于java虛擬機(jī)作用等相關(guān)內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • Java超詳細(xì)教你寫一個(gè)銀行存款系統(tǒng)案例

    Java超詳細(xì)教你寫一個(gè)銀行存款系統(tǒng)案例

    這篇文章主要介紹了怎么用Java來寫一個(gè)銀行的存款系統(tǒng),銀行存款主要有賬號(hào)和存款金額兩個(gè)屬性,感興趣的朋友跟隨文章往下看看吧
    2022-03-03

最新評(píng)論