SpringBoot自定義全局異常處理器的問題總結
一、介紹
Springboot
框架提供兩個注解幫助我們十分方便實現(xiàn)全局異常處理器
以及自定義異常
。
@ControllerAdvice
或@RestControllerAdvice
(推薦)@ExceptionHandler
二、實現(xiàn)
1. 定義全局異常處理器
定義GlobalExceptionHandler
類,攔截所有異常。@RestControllerAdvice
注解使得你可以在GlobalExceptionHandler
中處理異常,@ExceptionHandle
注解用于將指定異常綁定到處理的函數(shù)上。如下使用@ExceptionHandler(Exception.class)
即對所有異常進行捕獲處理。
@RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public RestErrorResponse exception(Exception e){ //record log log.error("系統(tǒng)異常{}", e.getMessage(),e); //decode errorException String errMessage = "系統(tǒng)異常"; return new RestErrorResponse(errMessage); } }
@Data @AllArgsConstructor public class RestErrorResponse implements Serializable { private String errMessage; }
事實上,寫到這里已經(jīng)可以用了,RestErrorResponse
用來承載錯誤信息到前端,因為@RestControllerAdvice
已經(jīng)包含了@ResponseBody
。
2. 自定義異常類
繼承RuntimeException
異常類寫一個自定義的異常類。這么做主要是能夠使用自定義的枚舉類來更優(yōu)雅的拋出錯誤。
@Data public class XueChengPlusException extends RuntimeException { private String errMessage; public XueChengPlusException() { super(); } public XueChengPlusException(String errMessage) { super(errMessage); this.errMessage = errMessage; } public static void cast(CommonError commonError){ throw new XueChengPlusException(commonError.getErrMessage()); } public static void cast(String errMessage){ throw new XueChengPlusException(errMessage); } }
@Getter public enum CommonError { UNKOWN_ERROR("執(zhí)行過程異常,請重試。"), PARAMS_ERROR("非法參數(shù)"), OBJECT_NULL("對象為空"), QUERY_NULL("查詢結果為空"), REQUEST_NULL("請求參數(shù)為空"); private String errMessage; private CommonError( String errMessage) { this.errMessage = errMessage; } }
同時,對于GlobalExceptionHandler
也要做一些修改,一方面處理自定義異常,另一方處理其余異常。
@RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(XueChengPlusException.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public RestErrorResponse customException(XueChengPlusException e){ //record log log.error("系統(tǒng)異常{}", e.getErrMessage(),e); //decode errorException String errMessage = e.getErrMessage(); return new RestErrorResponse(errMessage); } @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public RestErrorResponse exception(Exception e){ //record log log.error("系統(tǒng)異常{}", e.getMessage(),e); //decode errorException String errMessage = CommonError.UNKOWN_ERROR.getErrMessage(); return new RestErrorResponse(errMessage); } }
三、使用
在程序中任意地方拋出異常,controller
、service
、dao
層都可以,比如
throw new RuntimeException("價格不能為空且必須大于0");
這時走的就是
@ExceptionHandler(Exception.class) public RestErrorResponse exception(Exception e)
除此之外,可以這樣拋出自定義異常,比如
XueChengPlusException.cast(CommonError.PARAMS_ERROR);
XueChengPlusException.cast("其他的消息");
throw new XueChengPlusException(CommonError.OBJECT_NULL.getErrMessage());
throw new XueChengPlusException("其他的消息");
這時走的就是
@ExceptionHandler(XueChengPlusException.class) public RestErrorResponse customException(XueChengPlusException e)
四、疑問
Q:疑問,XueChengPlusException異常類繼承自RuntimeException ,而RuntimeException 繼承自Exception,為什么觸發(fā)customException而不是exception?
在這個全局異常處理器中,當拋出一個XueChengPlusException
異常時,它會被customException(XueChengPlusException e)
方法處理,而不是exception(Exception e)
方法。
這是因為Spring框架的異常處理機制會優(yōu)先匹配最具體的異常類型。在您的代碼中,XueChengPlusException
是RuntimeException
(以及Exception
)的子類,因此它更具體。所以,當拋出一個XueChengPlusException
異常時,Spring會優(yōu)先調用處理XueChengPlusException
的方法,而不是處理Exception
的方法。
這種行為確實表明全局異常處理器有一定的優(yōu)先級和覆蓋邏輯。具體來說,處理器會優(yōu)先處理更具體的異常類型,如果沒有找到匹配的處理器,那么它會尋找處理更一般異常類型的處理器。
到此這篇關于SpringBoot自定義全局異常處理器的文章就介紹到這了,更多相關SpringBoot全局異常處理器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用shardingsphere對SQLServer坑的解決
本文主要介紹了使用shardingsphere對SQLServer坑的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03Spring Boot自定義配置實現(xiàn)IDE自動提示功能
這篇文章主要介紹了Spring Boot自定義配置實現(xiàn)IDE自動提示功能,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08Java構造方法 super 及自定義異常throw合集詳解用法
異常是程序中的一些錯誤,但不是所有錯誤都是異常,且錯誤有時候是可以避免的,super可以理解為是指向自己超(父)類對象的一個指針,而這個超類指的是離自己最近的一個父類,構造器也叫構造方法、構造函數(shù),是一種特殊類型的方法,負責類中成員變量(域)的初始化2021-10-10