成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

你以為Spring Boot統一異常處理能攔截所有的異常?

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

[[396961]]

通常我們在Spring Boot中設置的統一異常處理只能處理Controller拋出的異常。有些請求還沒到Controller就出異常了,而這些異常不能被統一異常捕獲,例如Servlet容器的某些異常。今天我在項目開發中就遇到了一個,這讓我很不爽,因為它返回的錯誤信息格式不能統一處理,我決定找個方案解決這個問題。

ErrorPageFilter

Whitelabel Error Page

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

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

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

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

異常屬性

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

默認錯誤頁面

通常Spring Boot出現異常默認會跳轉到/error進行處理,而/error的相關邏輯則是由BasicErrorController實現的。

  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<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. // 其它省略 

而對應的配置:

  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())); 

所以我們只需要重新實現一個ErrorController并注入Spring IoC就可以替代默認的處理機制。而且我們可以很清晰的發現這個BasicErrorController不但是ErrorController的實現而且是一個控制器,如果我們讓控制器的方法拋異常,肯定可以被自定義的統一異常處理。所以我對BasicErrorController進行了改造:

  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.     } 

 

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

 

責任編輯:武曉燕 來源: 碼農小胖哥
相關推薦

2025-02-13 00:34:22

Spring對象系統

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統一處理

2023-11-28 14:32:04

2024-09-25 08:10:00

Spring后端

2021-04-20 10:50:38

Spring Boot代碼Java

2019-08-22 14:02:00

Spring BootRestful APIJava

2024-08-09 08:25:32

Spring流程注解

2023-09-24 13:55:42

Spring應用程序

2022-10-26 07:14:25

Spring 6Spring業務

2022-05-30 08:03:06

后端參數校驗異常處理

2023-07-10 08:00:13

架構Rest返回值

2011-05-24 09:22:44

Spring3異常處理

2020-03-16 17:20:02

異常處理Spring Boot

2020-03-11 08:00:00

.NET異常處理編程語言

2023-11-30 07:00:56

SpringBoot處理器

2024-10-28 08:32:22

統一接口響應SpringBoot響應框架

2022-03-15 21:38:29

sentry微服務監控
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91人人视频在线观看 | 国产高清免费 | 亚洲码欧美码一区二区三区 | 操到爽 | 天天操天天射天天舔 | 日韩一区在线播放 | 奇米av| 日韩成人中文字幕 | 国产精品日日做人人爱 | 国产综合久久 | 久久a久久| 久久av综合 | 乳色吐息在线观看 | 国产精品1区2区 | 日日操操 | 99精品视频在线观看 | 狠狠狠色丁香婷婷综合久久五月 | 国产日产精品一区二区三区四区 | 中文字幕一区二区三区在线观看 | 一区二区三区四区在线视频 | 精品二区 | 日韩av一区二区在线观看 | 91免费版在线观看 | 亚洲精品白浆高清久久久久久 | 日本一区二区三区在线观看 | 极品的亚洲 | 欧美精品日韩 | 国产一区二区在线播放 | 97偷拍视频 | 色嗨嗨| 日韩欧美国产不卡 | 美女国产 | 91精品www | 欧美日韩国产一区二区三区 | 一二三四在线视频观看社区 | 国产欧美一区二区三区在线看 | 国产精品一区二区视频 | 精品一区二区三区在线观看国产 | 91精品国产777在线观看 | 免费精品视频在线观看 | 欧美一级视频在线观看 |