Springboot自定義全局異常問題
更新時間:2024年05月17日 15:01:23 作者:納蘭雨天
這篇文章主要介紹了Springboot自定義全局異常問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Springboot自定義全局異常
自定義全局異常
bizException基礎(chǔ)包下封裝異常類
BizException
:運行時業(yè)務(wù)中出現(xiàn)的異常
/** * @Description : 運行時業(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 : 指定枚舉類中的錯誤類 * @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ù)錯誤的信息 * @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ù)異常的錯誤代碼接口
/** * @Description : 業(yè)務(wù)異常的錯誤代碼接口 * @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è)務(wù)異常枚舉類)
/** * @Description : 異常消息的枚舉類(此類屬于業(yè)務(wù)異常枚舉類) * @Param : * @Return : * @Author : l-jiahui * @Date : 2020-10-11 */ public enum BizExceptionCodeEnum implements BizExceptionCode{ // 已指明的異常,在異常使用時message并不返回前端,返回前端的為throw新的異常時指定的message SPECIFIED("-1","系統(tǒng)發(fā)生異常,請稍后重試"), // 常用業(yè)務(wù)異常 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對應(yīng)的對象信息 */ @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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+Idea熱部署實現(xiàn)流程解析
這篇文章主要介紹了SpringBoot+Idea熱部署實現(xiàn)流程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11SpringMVC實現(xiàn)返回響應(yīng)的項目實踐
本文主要介紹了SpringMVC實現(xiàn)返回響應(yīng)的項目實踐,包含返回靜態(tài)頁面,返回數(shù)據(jù),返回html片段等實例,具有一定的參考價值,感興趣的可以了解一下2024-02-02SpringBoot中定時任務(wù)@Scheduled的多線程使用詳解
這篇文章主要為大家詳細介紹了pring Boot定時任務(wù)@Scheduled的多線程原理以及如何加入線程池來處理定時任務(wù),感興趣的可以了解一下2023-04-04使用Mybatis Plus整合多數(shù)據(jù)源和讀寫分離的詳細過程
這篇文章主要介紹了Mybatis Plus整合多數(shù)據(jù)源和讀寫分離的詳細過程,mybatisplus可以整合阿里的分布式事務(wù)組件seata,本文通過示例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-09-09