java封裝全局異常處理深入詳解
1 定義錯(cuò)誤碼類
? 可以定義各種錯(cuò)誤碼枚舉,比如業(yè)務(wù),系統(tǒng)相關(guān)的報(bào)錯(cuò)信息
/** * 錯(cuò)誤代碼 * 錯(cuò)誤碼 * * @author leovany * @date 2023/09/23 */ public enum ErrorCode { SUCCESS(0, "success", ""), ERROR_PARAMS(40000, "請(qǐng)求參數(shù)錯(cuò)誤", ""), ERROR_NULL(40001, "請(qǐng)求數(shù)據(jù)為空", ""), ERROR_LOGIN(40100, "未登錄", ""), ERROR_NO_AUTH(41001, "無(wú)權(quán)限", ""), ERROR_SYSTEM(50000, "系統(tǒng)內(nèi)部異常", "") ; /** * 錯(cuò)誤碼ID */ private final int code; /** * 錯(cuò)誤碼信息 */ private final String message; /** * 錯(cuò)誤碼描述(詳情) */ private final String description; ErrorCode(int code, String message, String description) { this.code = code; this.message = message; this.description = description; } public int getCode() { return code; } public String getMessage() { return message; } public String getDescription() { return description; } }
2 定義業(yè)務(wù)異常類
相對(duì)于 java 的異常類,支持更多字段
擴(kuò)展了
code
和description
兩個(gè)字段- 自定義構(gòu)造函數(shù),更靈活 / 快捷的設(shè)置字段
import com.leovany.usercenter.common.ErrorCode; /** * 業(yè)務(wù)異常 * 自定義業(yè)務(wù)異常類 * * @author leovany * @date 2023/09/23 */ public class BusinessException extends RuntimeException { /** * 錯(cuò)誤碼 */ private final int code; /** * 描述 */ private final String description; /** * 業(yè)務(wù)異常 * * @param message 信息 * @param code 錯(cuò)誤碼 * @param description 描述 */ public BusinessException(String message, int code, String description) { super(message); this.code = code; this.description = description; } /** * 業(yè)務(wù)異常 * * @param errorCode 錯(cuò)誤代碼 */ public BusinessException(ErrorCode errorCode) { super(errorCode.getMessage()); this.code = errorCode.getCode(); this.description = errorCode.getDescription(); } /** * 業(yè)務(wù)異常 * * @param errorCode 錯(cuò)誤代碼 * @param description 描述 */ public BusinessException(ErrorCode errorCode, String description) { super(errorCode.getMessage()); this.code = errorCode.getCode(); this.description = description; } public int getCode() { return code; } public String getDescription() { return description; } }
3 全局異常處理器
通過(guò)Spring AOP實(shí)現(xiàn),在調(diào)用方法前后進(jìn)行額外的處理
作用
- 捕獲代碼中所有的異常,讓前端得到更詳細(xì)的業(yè)務(wù)報(bào)錯(cuò)信息
- 屏蔽掉項(xiàng)目框架本身的異常,不暴露服務(wù)器的內(nèi)部狀態(tài)
- 集中處理,比如還可以做記錄日志
import com.leovany.usercenter.common.ResultVO; import com.leovany.usercenter.common.ErrorCode; import com.leovany.usercenter.common.ResultUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * 全局異常處理類 */ @RestControllerAdvice @Slf4j public class GlobalExceptionHandler { /** * 處理異常-BusinessException * @param e * @return */ @ExceptionHandler(BusinessException.class) public ResultVO<?> businessExceptionHandler(BusinessException e){ log.error("businessException:" + e.getMessage(),e); return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription()); } /** * 處理異常-RuntimeException * @param e * @return */ @ExceptionHandler(RuntimeException.class) public ResultVO<?> runtimeExceptionHandler(RuntimeException e){ log.error("runtimeException:" + e); return ResultUtils.error(ErrorCode.ERROR_SYSTEM,e.getMessage()); } }
4 使用
? throw new BusinessException
可以在方法中,任意地方拋出,很方便
- 示例代碼
@PostMapping("/login") public ResultVO<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) { String userAccount = userLoginRequest.getUserAccount(); String userPassword = userLoginRequest.getUserPassword(); if (StringUtils.isAnyBlank(userAccount, userPassword)) { throw new BusinessException(ErrorCode.ERROR_PARAMS); } User user = userService.doLogin(userAccount, userPassword, request); return ResultUtils.success(user); }
- 代碼對(duì)比
5 前端請(qǐng)求效果
總結(jié)
通過(guò)封裝全局異常處理,對(duì)異常信息做了統(tǒng)一處理,讓前端得到更詳細(xì)的業(yè)務(wù)信息,同時(shí)保證系統(tǒng)的安全性(不會(huì)暴露系統(tǒng)內(nèi)部信息),在代碼上對(duì)參數(shù)校驗(yàn)等方面提供更加方便的形式。
以上就是java封裝全局異常處理深入詳解的詳細(xì)內(nèi)容,更多關(guān)于java封裝全局異常處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 基于java.lang.IllegalArgumentException異常報(bào)錯(cuò)問(wèn)題及解決
- IDEA 中使用 ECJ 編譯出現(xiàn) java.lang.IllegalArgumentException的錯(cuò)誤問(wèn)題
- 如何解決Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X
- java捕獲AOP級(jí)別的異常并將其傳遞到Controller層
- Java 8中Collectors.toMap空指針異常源碼解析
- java.lang.IllegalArgumentException:Invalid character found異常解決
相關(guān)文章
Java應(yīng)用服務(wù)器之tomcat會(huì)話復(fù)制集群配置的示例詳解
這篇文章主要介紹了Java應(yīng)用服務(wù)器之tomcat會(huì)話復(fù)制集群配置的相關(guān)知識(shí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java代碼性能測(cè)試實(shí)戰(zhàn)之ContiPerf安裝使用
這篇文章主要為大家介紹了Java代碼性能測(cè)試實(shí)戰(zhàn)之ContiPerf安裝使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Spring MVC數(shù)據(jù)綁定概述及原理詳解
這篇文章主要介紹了Spring MVC數(shù)據(jù)綁定概述及原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java程序員必須知道的5個(gè)JVM命令行標(biāo)志
這篇文章主要介紹了每個(gè)Java程序員必須知道的5個(gè)JVM命令行標(biāo)志,需要的朋友可以參考下2015-03-03淺談web服務(wù)器項(xiàng)目中靜態(tài)請(qǐng)求和動(dòng)態(tài)請(qǐng)求處理
這篇文章主要介紹了淺談web服務(wù)器項(xiàng)目中靜態(tài)請(qǐng)求和動(dòng)態(tài)請(qǐng)求處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07spring cloud gateway集成hystrix實(shí)戰(zhàn)篇
這篇文章主要介紹了spring cloud gateway集成hystrix實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07