91在线一级黄片|91视频在线观看18|成人夜间呦呦网站|91资源欧美日韩超碰|久久最新免费精品视频一区二区三区|国产探花视频在线观看|黄片真人免费三级片毛片|国产人无码视频在线|精品成人影视无码三区|久久视频爱久久免费精品

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

新聞中心

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

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

ErrorPageFilter

Whitelabel Error Page

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

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

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

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

異常屬性

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

默認(rèn)錯(cuò)誤頁(yè)面

通常Spring Boot出現(xiàn)異常默認(rèn)會(huì)跳轉(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.     //返回錯(cuò)誤頁(yè)面 
  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. // 其它省略 

而對(duì)應(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)一個(gè)ErrorController并注入Spring IoC就可以替代默認(rèn)的處理機(jī)制。而且我們可以很清晰的發(fā)現(xiàn)這個(gè)BasicErrorController不但是ErrorController的實(shí)現(xiàn)而且是一個(gè)控制器,如果我們讓控制器的方法拋異常,肯定可以被自定義的統(tǒng)一異常處理。所以我對(duì)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.     } 

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


標(biāo)題名稱:你以為SpringBoot統(tǒng)一異常處理能攔截所有的異常?
當(dāng)前URL:http://m.jiaoqi3.com/article/djoghgo.html