Springboot自定義全局異常問題
更新時間:2024年05月17日 15:01:23 作者:納蘭雨天
這篇文章主要介紹了Springboot自定義全局異常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Springboot自定義全局異常
自定義全局異常
bizException基礎包下封裝異常類
BizException:運行時業(yè)務中出現(xiàn)的異常
/**
* @Description : 運行時業(yè)務中出現(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 : 指定枚舉類中的錯誤類
* @Param : [errorCode]
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public BizException(final BizExceptionCode exceptionCode) {
super(exceptionCode.getMessage());
this.code = exceptionCode.getCode();
}
/**
* @Description : 指定具體業(yè)務錯誤的信息
* @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è)務異常的錯誤代碼接口
/**
* @Description : 業(yè)務異常的錯誤代碼接口
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public interface BizExceptionCode {
/**
* @Description : 獲取錯誤代碼
* @Param : []
* @Return : java.lang.String
* @Author : l-jiahui
* @Date : 2020-10-11
*/
String getCode();
/**
* @Description : 獲取錯誤信息
* @Param : []
* @Return : java.lang.String
* @Author : l-jiahui
* @Date : 2020-10-11
*/
String getMessage();
}BizExceptionCodeEnum:異常消息的枚舉類(此類屬于業(yè)務異常枚舉類)
/**
* @Description : 異常消息的枚舉類(此類屬于業(yè)務異常枚舉類)
* @Param :
* @Return :
* @Author : l-jiahui
* @Date : 2020-10-11
*/
public enum BizExceptionCodeEnum implements BizExceptionCode{
// 已指明的異常,在異常使用時message并不返回前端,返回前端的為throw新的異常時指定的message
SPECIFIED("-1","系統(tǒng)發(fā)生異常,請稍后重試"),
// 常用業(yè)務異常
USER_NAME_NULL("-1","用戶名不能為空,請重新輸入!"),
USER_PASSWORD_NULL("-1","密碼不能為空,請重新輸入!"),
USER_PASSWORD_WRONG("-1","密碼錯誤,請檢查后重新輸入!"),
PAGE_NUM_NULL("4001","頁碼不能為空"),
PAGE_SIZE_NULL("4002","頁數(shù)不能為空"),
SEARCH_NULL("4004","搜索條件不能為空,請檢查后重新輸入!"),
NO_LOGIN("3001", "用戶未進行登錄")
;
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)一錯誤信息
*
* @param response response對象
* @return 返回map對應的對象信息
*/
@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;
}
}總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java并發(fā)系列之CyclicBarrier源碼分析
這篇文章主要為大家詳細分析了Java并發(fā)系列之CyclicBarrier源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Spring關閉Tomcat Servlet容器時內(nèi)存泄漏問題解決方案
這篇文章主要介紹了Spring關閉Tomcat Servlet容器時內(nèi)存泄漏問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10
深入理解Java運行時數(shù)據(jù)區(qū)_動力節(jié)點Java學院整理
這篇文章主要介紹了Java運行時數(shù)據(jù)區(qū)的相關知識,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06

