SpringBoot自定義全局異常處理器的問(wèn)題總結(jié)
一、介紹
Springboot框架提供兩個(gè)注解幫助我們十分方便實(shí)現(xiàn)全局異常處理器以及自定義異常。
@ControllerAdvice或@RestControllerAdvice(推薦)@ExceptionHandler
二、實(shí)現(xiàn)
1. 定義全局異常處理器
定義GlobalExceptionHandler類,攔截所有異常。@RestControllerAdvice注解使得你可以在GlobalExceptionHandler 中處理異常,@ExceptionHandle注解用于將指定異常綁定到處理的函數(shù)上。如下使用@ExceptionHandler(Exception.class)即對(duì)所有異常進(jìn)行捕獲處理。
@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;
}事實(shí)上,寫到這里已經(jīng)可以用了,RestErrorResponse 用來(lái)承載錯(cuò)誤信息到前端,因?yàn)?code>@RestControllerAdvice已經(jīng)包含了@ResponseBody。
2. 自定義異常類
繼承RuntimeException 異常類寫一個(gè)自定義的異常類。這么做主要是能夠使用自定義的枚舉類來(lái)更優(yōu)雅的拋出錯(cuò)誤。
@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í)行過(guò)程異常,請(qǐng)重試。"),
PARAMS_ERROR("非法參數(shù)"),
OBJECT_NULL("對(duì)象為空"),
QUERY_NULL("查詢結(jié)果為空"),
REQUEST_NULL("請(qǐng)求參數(shù)為空");
private String errMessage;
private CommonError( String errMessage) {
this.errMessage = errMessage;
}
}同時(shí),對(duì)于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("價(jià)格不能為空且必須大于0");這時(shí)走的就是
@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("其他的消息");這時(shí)走的就是
@ExceptionHandler(XueChengPlusException.class)
public RestErrorResponse customException(XueChengPlusException e)四、疑問(wèn)
Q:疑問(wèn),XueChengPlusException異常類繼承自RuntimeException ,而RuntimeException 繼承自Exception,為什么觸發(fā)customException而不是exception?
在這個(gè)全局異常處理器中,當(dāng)拋出一個(gè)XueChengPlusException異常時(shí),它會(huì)被customException(XueChengPlusException e)方法處理,而不是exception(Exception e)方法。
這是因?yàn)镾pring框架的異常處理機(jī)制會(huì)優(yōu)先匹配最具體的異常類型。在您的代碼中,XueChengPlusException是RuntimeException(以及Exception)的子類,因此它更具體。所以,當(dāng)拋出一個(gè)XueChengPlusException異常時(shí),Spring會(huì)優(yōu)先調(diào)用處理XueChengPlusException的方法,而不是處理Exception的方法。
這種行為確實(shí)表明全局異常處理器有一定的優(yōu)先級(jí)和覆蓋邏輯。具體來(lái)說(shuō),處理器會(huì)優(yōu)先處理更具體的異常類型,如果沒有找到匹配的處理器,那么它會(huì)尋找處理更一般異常類型的處理器。

到此這篇關(guān)于SpringBoot自定義全局異常處理器的文章就介紹到這了,更多相關(guān)SpringBoot全局異常處理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中如何判斷JSONObject是否存在某個(gè)Key
這篇文章主要介紹了java中如何判斷JSONObject是否存在某個(gè)Key,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
使用shardingsphere對(duì)SQLServer坑的解決
本文主要介紹了使用shardingsphere對(duì)SQLServer坑的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Spring Boot自定義配置實(shí)現(xiàn)IDE自動(dòng)提示功能
這篇文章主要介紹了Spring Boot自定義配置實(shí)現(xiàn)IDE自動(dòng)提示功能,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Java構(gòu)造方法 super 及自定義異常throw合集詳解用法
異常是程序中的一些錯(cuò)誤,但不是所有錯(cuò)誤都是異常,且錯(cuò)誤有時(shí)候是可以避免的,super可以理解為是指向自己超(父)類對(duì)象的一個(gè)指針,而這個(gè)超類指的是離自己最近的一個(gè)父類,構(gòu)造器也叫構(gòu)造方法、構(gòu)造函數(shù),是一種特殊類型的方法,負(fù)責(zé)類中成員變量(域)的初始化2021-10-10

