springboot 自定義異常并捕獲異常返給前端的實(shí)現(xiàn)代碼
背景
在開發(fā)中,如果用try catch的方式,每個(gè)方法都需要單獨(dú)實(shí)現(xiàn),為了方便分類異常,返回給前端,采用了@ControllerAdvice注解和繼承了RuntimeException的方式來(lái)實(shí)現(xiàn)。
實(shí)現(xiàn)內(nèi)容
捕獲了三類異常
1.業(yè)務(wù)異常
BusinessException
2.系統(tǒng)異常
SystemException
3.其他異常
利用@ExceptionHandler(RuntimeException.class)去捕獲
ExceptionAdvice類捕獲以上三類異常,并返回自定義類型格式數(shù)據(jù)
實(shí)現(xiàn)代碼
業(yè)務(wù)異常BusinessException類實(shí)現(xiàn)方式,繼承RuntimeException
public class BusinessException extends RuntimeException { /** * 錯(cuò)誤編碼 */ private String code; public BusinessException() { super(); } public BusinessException(String message) { super(message); } public BusinessException(String code, String message) { super(message); this.code = code; } public BusinessException(Throwable cause) { super(cause); } public BusinessException(String message, Throwable cause) { super(message, cause); } public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String getMessage() { return super.getMessage(); } @Override public String toString() { return this.code + ":" + this.getMessage(); } }
系統(tǒng)異常SystemException類實(shí)現(xiàn)方式,繼承RuntimeException,同業(yè)務(wù)異常類的實(shí)現(xiàn)方式一樣
public class SystemException extends RuntimeException { /** * 錯(cuò)誤編碼 */ private String code; public SystemException() { super(); } public SystemException(String message) { super(message); } public SystemException(String code, String message) { super(message); this.code = code; } public SystemException(Throwable cause) { super(cause); } public SystemException(String message, Throwable cause) { super(message, cause); } public SystemException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } @Override public String getMessage() { return super.getMessage(); } @Override public String toString() { return this.code + ":" + this.getMessage(); } }
ExceptionAdvice類,采用增強(qiáng)Controller注解 @ControllerAdvice的方式來(lái)實(shí)現(xiàn)
1.方法名稱和返回類型都可以根據(jù)自己需要定義;
2.采用注解@ExceptionHandler,就是捕獲的異常類型,我們只需要把需要捕獲異常類型寫進(jìn)來(lái)就好
ExceptionAdvice 具體代碼實(shí)現(xiàn)如下:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public class ExceptionAdvice { public static Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class); @ResponseBody @ExceptionHandler(SystemException.class) public Result handleException(Exception e) { logger.error("系統(tǒng)異常信息:", e); Result result = new Result(); if (e instanceof BusinessException) { e = (BusinessException) e; result.setCode(((BusinessException) e).getCode()); } result.setFailed(e.getMessage()); return result; } @ExceptionHandler(RuntimeException.class) @ResponseBody public Result handleException(RuntimeException e) { logger.error("異常信息:", e.getMessage()); Result result = new Result(); result.setStatus(500); result.setMessage(e.getMessage()); return result; } @ExceptionHandler(BusinessException.class) @ResponseBody public AjaxJson doBusinessException(Exception e) { AjaxJson ajaxJson = new AjaxJson(); logger.error("業(yè)務(wù)異常消息:", e.getMessage()); ajaxJson.setRet(-1); ajaxJson.setMsg(e.getMessage()); return ajaxJson; } }
測(cè)試代碼
1.我們捕獲一個(gè)業(yè)務(wù)異常BusinessException,輸出aaa
2.捕獲系統(tǒng)異常
throw new SystemException("aaaa");
3.其他的try catch的異常,這個(gè)就可以捕獲了
到此這篇關(guān)于springboot 自定義異常并捕獲異常返給前端的文章就介紹到這了,更多相關(guān)springboot 自定義異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA通過(guò)Filter實(shí)現(xiàn)允許服務(wù)跨域請(qǐng)求的方法
這里的域指的是這樣的一個(gè)概念:我們認(rèn)為若協(xié)議 + 域名 + 端口號(hào)均相同,那么就是同域即我們常說(shuō)的瀏覽器請(qǐng)求的同源策略。這篇文章主要介紹了JAVA通過(guò)Filter實(shí)現(xiàn)允許服務(wù)跨域請(qǐng)求,需要的朋友可以參考下2018-11-11帶你用Java方法輕松實(shí)現(xiàn)樹的同構(gòu)
給定兩棵樹T1和T2。如果T1可以通過(guò)若干次左右孩子互換就變成T2,則我們稱兩棵樹是“同構(gòu)”的。例如圖1給出的兩棵樹就是同構(gòu)的,因?yàn)槲覀儼哑渲幸豢脴涞慕Y(jié)點(diǎn)A、B、G的左右孩子互換后,就得到另外一棵樹2021-06-06Java報(bào)錯(cuò):Error:java:?程序包org.springframework.boot不存在解決辦法
建完springboot項(xiàng)目時(shí),點(diǎn)擊啟動(dòng),有可能會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于Java報(bào)錯(cuò):Error:java:?程序包org.springframework.boot不存在的解決辦法,需要的朋友可以參考下2024-02-02Springboot項(xiàng)目對(duì)數(shù)據(jù)庫(kù)用戶名密碼實(shí)現(xiàn)加密過(guò)程解析
這篇文章主要介紹了Springboot項(xiàng)目對(duì)數(shù)據(jù)庫(kù)用戶名密碼實(shí)現(xiàn)加密過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Mybatis中注入執(zhí)行sql查詢、更新、新增及建表語(yǔ)句案例代碼
這篇文章主要介紹了Mybatis中注入執(zhí)行sql查詢、更新、新增以及建表語(yǔ)句,主要說(shuō)明一個(gè)另類的操作,注入sql,并使用mybatis執(zhí)行,結(jié)合案例代碼詳解講解,需要的朋友可以參考下2023-02-02基于rocketmq的有序消費(fèi)模式和并發(fā)消費(fèi)模式的區(qū)別說(shuō)明
這篇文章主要介紹了基于rocketmq的有序消費(fèi)模式和并發(fā)消費(fèi)模式的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring JdbcTemplate實(shí)現(xiàn)添加與查詢方法詳解
JdbcTemplate是Spring框架自帶的對(duì)JDBC操作的封裝,目的是提供統(tǒng)一的模板方法使對(duì)數(shù)據(jù)庫(kù)的操作更加方便、友好,效率也不錯(cuò),這篇文章主要介紹了Spring?JdbcTemplate執(zhí)行數(shù)據(jù)庫(kù)操作,需要的朋友可以參考下2022-11-11