自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

你以為Spring Boot統(tǒng)一異常處理能攔截所有的異常?

開發(fā) 架構(gòu)
通常我們在Spring Boot中設(shè)置的統(tǒng)一異常處理只能處理Controller拋出的異常。有些請求還沒到Controller就出異常了,而這些異常不能被統(tǒng)一異常捕獲,例如Servlet容器的某些異常。

[[396961]]

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

ErrorPageFilter

Whitelabel Error Page

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

  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在啟動時(shí)會注冊一個(gè)ErrorPageFilter,當(dāng)Servlet發(fā)生異常時(shí),該過濾器就會攔截處理,將異常根據(jù)不同的策略進(jìn)行處理:當(dāng)異常已經(jīng)在處理的話直接處理,否則轉(zhuǎn)發(fā)給對應(yīng)的錯(cuò)誤頁面。有興趣的可以去看下源碼,邏輯不復(fù)雜,這里就不貼了。

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

異常屬性

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

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

通常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.     //返回錯(cuò)誤頁面 
  5.   @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 
  6.  public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { 
  7.   HttpStatus status = getStatus(request); 
  8.   Map<String, Object> 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<Map<String, Object>> error(HttpServletRequest request) { 
  17.   HttpStatus status = getStatus(request); 
  18.   if (status == HttpStatus.NO_CONTENT) { 
  19.    return new ResponseEntity<>(status); 
  20.   } 
  21.   Map<String, Object> 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<ErrorViewResolver> 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)一異常處理。所以我對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<Map<String, Object>> 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)一處理,保證整個(gè)應(yīng)用的異常處理對外保持一個(gè)統(tǒng)一的門面。

 

責(zé)任編輯:武曉燕 來源: 碼農(nóng)小胖哥
相關(guān)推薦

2025-02-13 00:34:22

Spring對象系統(tǒng)

2017-05-18 14:14:25

過濾器Spring ClouZuul

2022-04-08 16:27:48

SpringBoot異常處理

2017-05-19 15:13:05

過濾器Spring ClouZuul

2017-07-31 15:47:50

Zuul統(tǒng)一處理

2024-09-25 08:10:00

Spring后端

2023-11-28 14:32:04

2021-04-20 10:50:38

Spring Boot代碼Java

2019-08-22 14:02:00

Spring BootRestful APIJava

2023-09-24 13:55:42

Spring應(yīng)用程序

2024-08-09 08:25:32

Spring流程注解

2022-10-26 07:14:25

Spring 6Spring業(yè)務(wù)

2011-05-24 09:22:44

Spring3異常處理

2023-07-10 08:00:13

架構(gòu)Rest返回值

2022-05-30 08:03:06

后端參數(shù)校驗(yàn)異常處理

2020-03-16 17:20:02

異常處理Spring Boot

2023-11-30 07:00:56

SpringBoot處理器

2020-03-11 08:00:00

.NET異常處理編程語言

2024-10-28 08:32:22

統(tǒng)一接口響應(yīng)SpringBoot響應(yīng)框架

2024-08-02 09:15:22

Spring捕捉格式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號