SpringBoot第七集:例外處理與整合JSR303校驗(2020最新最易懂)
一.SpringBoot全域例外
先講下什么是全域例外處理器?
全域例外處理器就是把整個系統的例外統一自動處理,程式員可以做到不用寫try... catch,SpringBoot內置有默認全域例外處理器,
Spring Boot對例外的處理有一套默認的機制,BasicErrorController處理默認例外轉發的或這error請求 :當應用中產生例外時,當從瀏覽器地址欄中訪問應用介面時,SpringBoot會獲取請求頭中資料,如果請求頭中的accept包含text/html資訊,產生例外時,Spring Boot會通過ModelAndView模型物件來裝載例外資訊,并以HTML的格式回傳;反之,請求頭中的accept不包含text/html時,Spring Boot則以JSON的格式回傳例外資訊,
例如:訪問一個未知介面資源(或后臺介面定義10/0的錯誤,回應的HTML結果如下)

例如:利用Postman測驗工具,訪問未知資源測驗:(可以嘗試使用其他插件工具:使用Chrome插件Restlet Client)

1.全域例外處理機制原始碼決議
BasicErrorController原始碼截取如下:
@RequestMapping("${server.error.path:${error.path:/error}}")請求的例外頁面地址為/error/下面的資源
當沒有自定義例外頁面時,默認按下方原始碼執行構建HTML或JSON回應給前臺,
1 @Controller 2 @RequestMapping("${server.error.path:${error.path:/error}}") 3 public class BasicErrorController extends AbstractErrorController { 4 /** 5 * 錯誤資訊處理器方法errorHtml,設定了請求頭Accpet值型別,如果包含text/html,即執行該方法 6 * @param request 請求物件 7 * @param response 回應物件 8 * @return 9 * MediaType.TEXT_HTML_VALUE的實際值就是一個字串“text/html” 10 */ 11 @RequestMapping(produces = MediaType.TEXT_HTML_VALUE) 12 public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { 13 // 獲取狀態碼 14 HttpStatus status = getStatus(request); 15 Map<String, Object> model = Collections 16 .unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML))); 17 // 回應狀態碼描述 18 response.setStatus(status.value()); 19 // 創建視圖模型物件 20 ModelAndView modelAndView = resolveErrorView(request, response, status, model); 21 return (modelAndView != null) ? modelAndView : new ModelAndView("error", model); 22 } 23 24 /** 25 * 錯誤資訊處理器方法error方法,設定了請求頭Accpet值型別,即沒有包含text/html執行該方法 26 * @param request 請求物件 27 * @param response 回應物件 28 */ 29 @RequestMapping 30 public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { 31 HttpStatus status = getStatus(request); 32 if (status == HttpStatus.NO_CONTENT) { 33 return new ResponseEntity<>(status); 34 } 35 Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL)); 36 return new ResponseEntity<>(body, status); 37 } 38 39 }
我們可以自定義友好的例外頁面,但必須是放在資源/error/目錄下,資源目錄存放默認地址可選
src/main/resources/static/,src/main/resources/resources/,src/main/resources/public/,src/main/templates/
說明:前三者是靜態資源目錄,頁面我們使用模板引擎,因此如果需要自定義錯誤頁面,那么需要放在src/main/templates/error目錄下(當然所有的前提是,沒有更改默認配置,SpringBoot默認加載其中的錯誤頁面),且錯誤頁面命名必須以狀態碼方式,SpringBoot默認錯誤視圖決議器DefaultErrorViewResolver原始碼決議如下:
1 public class DefaultErrorViewResolver implements ErrorViewResolver, Ordered { 2 3 private static final Map<Series, String> SERIES_VIEWS; 4 5 // 靜態初始化錯誤狀態型別:4xx 或 5xx 6 static { 7 Map<Series, String> views = new EnumMap<>(Series.class); 8 views.put(Series.CLIENT_ERROR, "4xx"); 9 views.put(Series.SERVER_ERROR, "5xx"); 10 SERIES_VIEWS = Collections.unmodifiableMap(views); 11 } 12 13 14 // 決議錯誤視圖 15 @Override 16 public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) { 17 // 獲取錯誤狀態碼例如:404,轉為字串呼叫方法resolve(決議方法) 18 ModelAndView modelAndView = resolve(String.valueOf(status.value()), model); 19 if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) { 20 modelAndView = resolve(SERIES_VIEWS.get(status.series()), model); 21 } 22 return modelAndView; 23 } 24 25 // 決議處理方法 26 private ModelAndView resolve(String viewName, Map<String, Object> model) { 27 // 拼接錯誤視圖訪問前綴:error/500 28 String errorViewName = "error/" + viewName; 29 TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, 30 this.applicationContext); 31 if (provider != null) { 32 return new ModelAndView(errorViewName, model); 33 } 34 // 呼叫決議資源:傳入error/500 35 return resolveResource(errorViewName, model); 36 } 37 38 // 決議資源 39 private ModelAndView resolveResource(String viewName, Map<String, Object> model) { 40 for (String location : this.resourceProperties.getStaticLocations()) { 41 try { 42 // 獲取資決議 43 Resource resource = this.applicationContext.getResource(location); 44 // 創建決議檔案為:error/500.html 45 resource = resource.createRelative(viewName + ".html"); 46 if (resource.exists()) { 47 return new ModelAndView(new HtmlResourceView(resource), model); 48 } 49 } 50 catch (Exception ex) { 51 } 52 } 53 return null; 54 } 55 }
關于模板引擎的整合,參考第九集:整合JSP和模板引擎
2.自定義例外頁面
1.在src/main/templates/error目錄下新建錯誤頁面:例如:404.html
2.測驗訪問,
1 <!DOCTYPE html> 2 <html xmlns:th="http://www.thymeleaf.org"> <!-- Thymeleaf模板約束 --> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Insert title here</title> 6 </head> 7 <body> 8 自定義404友好錯誤頁面!<br> 9 對不起,你訪問的資料被外星人盜竊了…… 10 </body> 11 </html>

3.自定義例外資訊
除了可以可以自定義友好例外頁面(HTML)外,我們也可以自定義例外處理資訊,改變默認的客戶端訪問介面產生的例外資訊,
由于作業中都是前后端分離開發模式,所以幾乎沒有直接回傳資源頁的需求,一般上都是回傳固定的回應格式JSON,如respCode、respMsg、data等,前端通過判斷respCode的值進行業務判斷,是彈窗還是跳轉頁面,
- 撰寫自定義例外類,封裝例外資訊(便于JSON轉換)
1 @Data 2 @NoArgsConstructor 3 @AllArgsConstructor 4 public class ExceptionResponseResult{ 5 @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") // 日期格式化 6 private Date timestamp;// 時間 7 private int respCode;// 狀態碼 8 private String respMsg;// 給用戶看的描述資訊 9 private String message;// 實際錯誤例外資訊 10 private String exceptionName;// 實際錯誤例外名字 11 private String path;// URI 12 private Object data;// 資料 13 }
- 撰寫全域例外處理器
a,撰寫一個全域例外處理器類
b,在類上添加注解@ControllerAdvice
@ControllerAdvice:作用:對所有控制器中,被@RequestMapping注解標注的方法,進行增強(也可以直接使用@RestControllerAdvice)
c,自定義例外處理方法,并使用注解@ExceptionHandler(Throwable.class),@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR),@ResponseBody
@ExceptionHandler(Throwable.class):例外處理器注解,通常配合@ControllerAdvice注解使用,作用是:對指定或滿足的例外型別實施攔截處理
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR):用于指定回應狀態碼的,HttpStatus是Spring內置的一個狀態碼列舉類,內定了詳細的狀態碼及描,
@ResponseBody:作用:將回應的結果轉為JSON資訊,如果使用了@RestControllerAdvice則方法無需使用@ResponseBody注解
1 // @ControllerAdvice 2 @RestControllerAdvice // 控制器類增強:可以對Controller中所有使用@RequestMapping注解的方法增強 3 public class GlobalExceptionHandler { 4 5 // 該注解是例外處理器注解,可以對指定例外型別處理,執行注解標注的方法(只要發生指定例外都會被攔截) 6 @ExceptionHandler(Throwable.class) 7 // 該注解用于指定例外處理方法執行后回應頁面的HTTP狀態碼,HttpStatus是Spring內置的一個狀態碼列舉類,內定了詳細的狀態碼及描述,當前獲取的是500 8 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)// 回應500 9 public Object exceptonResponse(Exception ex,HttpServletRequest request) { 10 ExceptionResponseResult resultError = new ExceptionResponseResult(); 11 resultError.setTimestamp(new Date());// 設定例外發生時間 12 resultError.setRespCode(0);// 可以選擇自定義列舉類,定義狀態碼 13 resultError.setRespMsg("服務器重繪例外,請稍后,,,");// 用戶看到的例外資訊 14 resultError.setMessage(ex.getMessage());// 實際發生的例外資訊 15 resultError.setExceptionName(ex.getClass().getName());// 實際例外的名字 16 resultError.setPath(request.getRequestURI());// 例外RUI 17 return resultError; 18 } 19 20 }
- 撰寫控制器Controller定義后臺錯誤
1 @Controller 2 public class HtmlController { 3 4 @RequestMapping("/indexHtml") 5 public String indexHtml(Model model) { 6 model.addAttribute("url","XSGE個人網站:http://www.xsge123.com"); 7 System.out.println("測驗"+(10/0)); 8 return "indexHtml"; 9 } 10 }
- 使用Postman測驗訪問
頁面訪問測驗結果:正常回應,但狀態碼是500
- 例外處理優化
在例外處理器方法中,判斷例外型別,定義更加細節的例外回應內容,
1 // 該注解是例外處理器注解,可以對指定例外型別處理,執行注解標注的方法(只要發生指定例外都會被攔截) 2 @ExceptionHandler(Throwable.class) 3 // 該注解用于指定例外處理方法執行后回應頁面的HTTP狀態碼,HttpStatus是Spring內置的一個狀態碼列舉類,內定了詳細的狀態碼及描述,當前獲取的是500 4 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)// 回應500 5 public Object exceptonResponse(Exception ex,HttpServletRequest request) { 6 ExceptionResponseResult resultError = new ExceptionResponseResult(); 7 if (ex instanceof NullPointerException) {// 如果捕獲的例外為控空指標例外 8 // ****設定例外資訊***** 9 } else if (ex instanceof ArithmeticException) { 10 // ****設定例外資訊***** 11 }// ***** 12 return resultError; 13 }
二.JSR-303資料校驗
在任何時候,當你要處理一個應用程式的業務邏輯,資料校驗是你必須要考慮和面對的事情,然后僅僅前端頁面的校驗就能保證安全了嗎?小朋友還是年輕,基礎的前端攻擊技術網上很多,所以,僅僅頁面資料校驗是不夠的,JSR是Java Specification Requests的縮寫,意思是Java 規范提案,是指向JCP(Java Community Process)提出新增一個標準化技術規范的正式請求,
JSR-303 是JAVA EE 6 中的一項子規范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的參考實作 , Hibernate Validator 提供了 JSR 303 規范中所有內置 constraint(約束) 的實作,除此之外還有一些附加的 constraint(約束),(注意:此實作與 Hibernate ORM 沒有任何關系),
校驗規則及使用方法:關注博主SSM整合之資料校驗!!!(目前資料已備沒時間寫,敬請期待!)等不及的可以查閱Githup官網,查看說明檔案,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/212678.html
標籤:Java
上一篇:Java修飾符型別
下一篇:ArrayList原始碼分析
