日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
你以為SpringBoot統(tǒng)一異常處理能攔截所有的異常?

通常我們在Spring Boot中設(shè)置的統(tǒng)一異常處理只能處理Controller拋出的異常。有些請求還沒到Controller就出異常了,而這些異常不能被統(tǒng)一異常捕獲,例如Servlet容器的某些異常。今天我在項(xiàng)目開發(fā)中就遇到了一個,這讓我很不爽,因?yàn)樗祷氐腻e誤信息格式不能統(tǒng)一處理,我決定找個方案解決這個問題。

ErrorPageFilter

Whitelabel Error Page

這類圖相信大家沒少見,Spring Boot 只要出錯,體現(xiàn)在頁面上的就是這個。如果你用Postman之類的測試出了異常則是:

 
 
 
 
  1.   "timestamp": "2021-04-29T22:45:33.231+0000", 
  2.   "status": 500, 
  3.   "message": "Internal Server Error", 
  4.   "path": "foo/bar" 

這個是怎么實(shí)現(xiàn)的呢?Spring Boot在啟動時會注冊一個ErrorPageFilter,當(dāng)Servlet發(fā)生異常時,該過濾器就會攔截處理,將異常根據(jù)不同的策略進(jìn)行處理:當(dāng)異常已經(jīng)在處理的話直接處理,否則轉(zhuǎn)發(fā)給對應(yīng)的錯誤頁面。有興趣的可以去看下源碼,邏輯不復(fù)雜,這里就不貼了。

另外當(dāng)一個 Servlet 拋出一個異常時,處理異常的Servlet可以從HttpServletRequest里面得到幾個屬性,如下:

異常屬性

我們可以從上面的幾個屬性中獲取異常的詳細(xì)信息。

默認(rèn)錯誤頁面

通常Spring Boot出現(xiàn)異常默認(rèn)會跳轉(zhuǎn)到/error進(jìn)行處理,而/error的相關(guān)邏輯則是由BasicErrorController實(shí)現(xiàn)的。

 
 
 
 
  1. @Controller 
  2. @RequestMapping("${server.error.path:${error.path:/error}}") 
  3. public class BasicErrorController extends AbstractErrorController { 
  4.     //返回錯誤頁面 
  5.   @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 
  6.  public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { 
  7.   HttpStatus status = getStatus(request); 
  8.   Map model = Collections 
  9.     .unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML))); 
  10.   response.setStatus(status.value()); 
  11.   ModelAndView modelAndView = resolveErrorView(request, response, status, model); 
  12.   return (modelAndView != null) ? modelAndView : new ModelAndView("error", model); 
  13.  } 
  14.     // 返回json 
  15.  @RequestMapping 
  16.  public ResponseEntity> error(HttpServletRequest request) { 
  17.   HttpStatus status = getStatus(request); 
  18.   if (status == HttpStatus.NO_CONTENT) { 
  19.    return new ResponseEntity<>(status); 
  20.   } 
  21.   Map body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL)); 
  22.   return new ResponseEntity<>(body, status); 
  23.  }   
  24. // 其它省略 

而對應(yīng)的配置:

 
 
 
 
  1. @Bean 
  2. @ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT) 
  3. public BasicErrorController basicErrorController(ErrorAttributes errorAttributes, 
  4.       ObjectProvider errorViewResolvers) { 
  5.    return new BasicErrorController(errorAttributes, this.serverProperties.getError(), 
  6.          errorViewResolvers.orderedStream().collect(Collectors.toList())); 

所以我們只需要重新實(shí)現(xiàn)一個ErrorController并注入Spring IoC就可以替代默認(rèn)的處理機(jī)制。而且我們可以很清晰的發(fā)現(xiàn)這個BasicErrorController不但是ErrorController的實(shí)現(xiàn)而且是一個控制器,如果我們讓控制器的方法拋異常,肯定可以被自定義的統(tǒng)一異常處理。所以我對BasicErrorController進(jìn)行了改造:

 
 
 
 
  1. @Controller 
  2. @RequestMapping("${server.error.path:${error.path:/error}}") 
  3. public class ExceptionController extends AbstractErrorController { 
  4.  
  5.  
  6.     public ExceptionController(ErrorAttributes errorAttributes) { 
  7.         super(errorAttributes); 
  8.     } 
  9.  
  10.  
  11.     @Override 
  12.     @Deprecated 
  13.     public String getErrorPath() { 
  14.         return null; 
  15.     } 
  16.  
  17.     @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 
  18.     public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { 
  19.         throw new RuntimeException(getErrorMessage(request)); 
  20.     } 
  21.  
  22.     @RequestMapping 
  23.     public ResponseEntity> error(HttpServletRequest request) { 
  24.         throw new RuntimeException(getErrorMessage(request)); 
  25.     } 
  26.  
  27.     private String getErrorMessage(HttpServletRequest request) { 
  28.         Object code = request.getAttribute("javax.servlet.error.status_code"); 
  29.         Object exceptionType = request.getAttribute("javax.servlet.error.exception_type"); 
  30.         Object message = request.getAttribute("javax.servlet.error.message"); 
  31.         Object path = request.getAttribute("javax.servlet.error.request_uri"); 
  32.         Object exception = request.getAttribute("javax.servlet.error.exception"); 
  33.  
  34.         return String.format("code: %s,exceptionType: %s,message: %s,path: %s,exception: %s", 
  35.                 code, exceptionType, message, path, exception); 
  36.     } 

直接拋異常,簡單省力!凡是這里捕捉的到的異常大部分還沒有經(jīng)過Controller,我們通過ExceptionController中繼也讓這些異常被統(tǒng)一處理,保證整個應(yīng)用的異常處理對外保持一個統(tǒng)一的門面。


名稱欄目:你以為SpringBoot統(tǒng)一異常處理能攔截所有的異常?
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/djoghgo.html