springboot 自定義異常並捕獲異常返給前端的實現代碼
背景
在開發中,如果用try catch的方式,每個方法都需要單獨實現,為瞭方便分類異常,返回給前端,采用瞭@ControllerAdvice註解和繼承瞭RuntimeException的方式來實現。
實現內容
捕獲瞭三類異常
1.業務異常
BusinessException
2.系統異常
SystemException
3.其他異常
利用@ExceptionHandler(RuntimeException.class)去捕獲
ExceptionAdvice類捕獲以上三類異常,並返回自定義類型格式數據
實現代碼
業務異常BusinessException類實現方式,繼承RuntimeException
public class BusinessException extends RuntimeException { /** * 錯誤編碼 */ 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(); } }
系統異常SystemException類實現方式,繼承RuntimeException,同業務異常類的實現方式一樣
public class SystemException extends RuntimeException { /** * 錯誤編碼 */ 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類,采用增強Controller註解 @ControllerAdvice的方式來實現
1.方法名稱和返回類型都可以根據自己需要定義;
2.采用註解@ExceptionHandler,就是捕獲的異常類型,我們隻需要把需要捕獲異常類型寫進來就好
ExceptionAdvice 具體代碼實現如下:
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("系統異常信息:", 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("業務異常消息:", e.getMessage()); ajaxJson.setRet(-1); ajaxJson.setMsg(e.getMessage()); return ajaxJson; } }
測試代碼
1.我們捕獲一個業務異常BusinessException,輸出aaa
2.捕獲系統異常
throw new SystemException("aaaa");
3.其他的try catch的異常,這個就可以捕獲瞭
到此這篇關於springboot 自定義異常並捕獲異常返給前端的文章就介紹到這瞭,更多相關springboot 自定義異常內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- java實現統一異常處理的示例
- 如何在SpringBoot項目裡進行統一異常處理
- SpringBoot中controller深層詳細講解
- Spring Boot統一處理全局異常的實戰教程
- Spring Boot全局統一異常處理器