Springboot自定義全局異常問題
更新時(shí)間:2024年05月17日 15:01:23 作者:納蘭雨天
這篇文章主要介紹了Springboot自定義全局異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Springboot自定義全局異常
自定義全局異常
bizException基礎(chǔ)包下封裝異常類
BizException:運(yùn)行時(shí)業(yè)務(wù)中出現(xiàn)的異常
/**
* @Description : 運(yùn)行時(shí)業(yè)務(wù)中出現(xiàn)的異常
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public class BizException extends RuntimeException {
private static final long serialVersionUID = -7864604160297181941L;
private final String code;
/**
* @Description : 指定枚舉類中的錯(cuò)誤類
* @Param : [errorCode]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public BizException(final BizExceptionCode exceptionCode) {
super(exceptionCode.getMessage());
this.code = exceptionCode.getCode();
}
/**
* @Description : 指定具體業(yè)務(wù)錯(cuò)誤的信息
* @Param : [detailedMessage]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public BizException(final String message) {
super(message);
this.code = BizExceptionCodeEnum.SPECIFIED.getCode();
}
public String getCode() {
return code;
}
}BizExceptionCode: 業(yè)務(wù)異常的錯(cuò)誤代碼接口
/**
* @Description : 業(yè)務(wù)異常的錯(cuò)誤代碼接口
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public interface BizExceptionCode {
/**
* @Description : 獲取錯(cuò)誤代碼
* @Param : []
* @Return : java.lang.String
* @Author : l-jiahui
* @Date : 2020-10-11
*/
String getCode();
/**
* @Description : 獲取錯(cuò)誤信息
* @Param : []
* @Return : java.lang.String
* @Author : l-jiahui
* @Date : 2020-10-11
*/
String getMessage();
}BizExceptionCodeEnum:異常消息的枚舉類(此類屬于業(yè)務(wù)異常枚舉類)
/**
* @Description : 異常消息的枚舉類(此類屬于業(yè)務(wù)異常枚舉類)
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public enum BizExceptionCodeEnum implements BizExceptionCode{
// 已指明的異常,在異常使用時(shí)message并不返回前端,返回前端的為throw新的異常時(shí)指定的message
SPECIFIED("-1","系統(tǒng)發(fā)生異常,請(qǐng)稍后重試"),
// 常用業(yè)務(wù)異常
USER_NAME_NULL("-1","用戶名不能為空,請(qǐng)重新輸入!"),
USER_PASSWORD_NULL("-1","密碼不能為空,請(qǐng)重新輸入!"),
USER_PASSWORD_WRONG("-1","密碼錯(cuò)誤,請(qǐng)檢查后重新輸入!"),
PAGE_NUM_NULL("4001","頁碼不能為空"),
PAGE_SIZE_NULL("4002","頁數(shù)不能為空"),
SEARCH_NULL("4004","搜索條件不能為空,請(qǐng)檢查后重新輸入!"),
NO_LOGIN("3001", "用戶未進(jìn)行登錄")
;
private final String code;
private final String message;
/**
* @Description :
* @Param : [code, message]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
BizExceptionCodeEnum(String code,String message){
this.code = code;
this.message = message;
}
@Override
public String getCode() {
return code;
}
@Override
public String getMessage() {
return message;
}
}全局捕獲異常和自定義全局捕獲異常以及404處理
/**
* @author l-jiahui
* extend of {@link ResponseEntityExceptionHandler} for handle all exception
* @Description: 全局捕獲異常和自定義全局捕獲異常
*/
@ControllerAdvice
public class GlobalControllerAdvice {
/**
* 攔截捕捉自定義異常 BizException.class
*
* @param bizException 自定義異常
* @return map
*/
@ResponseBody
@ExceptionHandler(value = BizException.class)
public Map<String, Object> myExceptionHandler(BizException bizException, HttpServletResponse response) {
Map<String, Object> map = new HashMap<>(16);
map.put("code", bizException.getCode());
map.put("msg", bizException.getMessage());
response.setStatus(Integer.parseInt(bizException.getCode()));
return map;
}
/**
* 增加處理404的統(tǒng)一錯(cuò)誤信息
*
* @param response response對(duì)象
* @return 返回map對(duì)應(yīng)的對(duì)象信息
*/
@ExceptionHandler(value = {NoHandlerFoundException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public Map<String, Object> notFoundException(HttpServletResponse response) {
Map<String, Object> map = new HashMap<>(16);
map.put("code", "404");
map.put("msg", "not found exception");
response.setStatus(404);
return map;
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java并發(fā)系列之CyclicBarrier源碼分析
這篇文章主要為大家詳細(xì)分析了Java并發(fā)系列之CyclicBarrier源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Spring關(guān)閉Tomcat Servlet容器時(shí)內(nèi)存泄漏問題解決方案
這篇文章主要介紹了Spring關(guān)閉Tomcat Servlet容器時(shí)內(nèi)存泄漏問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
IDEA實(shí)現(xiàn)一鍵部署項(xiàng)目到服務(wù)器過程
本文介紹了在阿里云服務(wù)器上通過開放2375端口、配置Docker及IDEA插件,實(shí)現(xiàn)項(xiàng)目打包、編寫Dockerfile、構(gòu)建鏡像和容器,并最終通過瀏覽器訪問部署服務(wù)的全過程2025-10-10
深入理解Java運(yùn)行時(shí)數(shù)據(jù)區(qū)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java運(yùn)行時(shí)數(shù)據(jù)區(qū)的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06
如何將JSON字符串?dāng)?shù)組轉(zhuǎn)對(duì)象集合
這篇文章主要介紹了如何將JSON字符串?dāng)?shù)組轉(zhuǎn)對(duì)象集合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

