SpringBoot實現全局異常的封裝和統(tǒng)一處理
前言
在Spring Boot應用中,全局異常的處理是一個非常重要的方面,它可以提高系統(tǒng)的穩(wěn)定性和用戶體驗。在這篇博客中,我們將介紹如何在Spring Boot中進行全局異常的封裝和統(tǒng)一處理。
全局異常處理能夠捕獲應用中所有未處理的異常,統(tǒng)一進行處理,防止異常信息泄露到客戶端,同時也能夠記錄異常信息以便后續(xù)的調試和分析。
接下來就用SpringBoot實例代碼實現一個簡單的全局異常攔截。
創(chuàng)建全局異常類
首先,我們需要創(chuàng)建一個自定義的全局異常類,繼承自RuntimeException,用于封裝業(yè)務異常信息。
/** 可直接進行throw該異常類進行返回信息 **/ public class CustomException extends RuntimeException { //錯誤碼枚舉 private final ErrorCode errorCode; public CustomException(ErrorCode errorCode) { super(errorCode.getMessage()); this.errorCode = errorCode; } public ErrorCode getErrorCode() { return errorCode; } }
創(chuàng)建錯誤碼枚舉
為了更好地封裝異常信息,我們創(chuàng)建一個錯誤碼的枚舉類,用于定義異常碼和異常信息。
public enum ErrorCode { SUCCESS("10000", "success"), SYSTEM_ERROR("500", "系統(tǒng)系統(tǒng),請聯(lián)系管理員"), NOT_FOUNT("404","未找到對應的資源"); // 其他錯誤碼... private final String code; private final String message; ErrorCode(String code, String message) { this.code = code; this.message = message; } public String getCode() { return code; } public String getMessage() { return message; } }
創(chuàng)建全局異常處理器
然后,我們創(chuàng)建一個全局異常處理器,使用@ControllerAdvice
注解,配合@ExceptionHandler
注解處理各種異常。
@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(CustomException.class) public ResponseEntity<Response> handleCustomException(CustomException ex) { ErrorCode errorCode = ex.getErrorCode(); return new ResponseEntity<>(Response.error(errorCode.getCode(), errorCode.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(Exception.class) public ResponseEntity<Response> handleOtherExceptions(Exception ex) { return new ResponseEntity<>(Response.error(), HttpStatus.INTERNAL_SERVER_ERROR); } }
創(chuàng)建錯誤響應類
最后,我們創(chuàng)建一個錯誤響應類,用于返回統(tǒng)一的錯誤格式。
@Data public class Response<T> { private String code; private String message; private T data; //默認的返回成功 public static Response success(){ Response response =new Response(); response.setCode(ErrorCode.SUCCESS.getCode()); response.setMessage(ErrorCode.SUCCESS.getMessage()); return response; } //帶內容的返回成功 public static <T> Response success(T data){ Response<T> response =new Response<T>(); response.setCode(ErrorCode.SUCCESS.getCode()); response.setData(data); response.setMessage(ErrorCode.SUCCESS.getMessage()); return response; } //默認的返回失敗 public static Response error(){ Response response =new Response(); response.setCode(ErrorCode.SYSTEM_ERROR.getCode()); response.setMessage(ErrorCode.SYSTEM_ERROR.getMessage()); return response; } //自定義的返回失敗 public static Response error(String code,String msg){ Response response =new Response(); response.setCode(code); response.setMessage(msg); return response; } }
測試
現在,我們可以在業(yè)務代碼中通過拋出CustomException來觸發(fā)全局異常處理,例如:
@RestController @RequestMapping("/api/test") public class TestConroller { @GetMapping("/success") public ResponseEntity<Response> successObj(){ return ResponseEntity.ok(Response.success("111111")); } @GetMapping("/error") public ResponseEntity<Response> error(String name) { //傳入的name等于張三時,拋出我們的異常處理。 if(StringUtils.equals(name,"zhangsan")){ throw new CustomException(ErrorCode.SYSTEM_ERROR); } return ResponseEntity.ok(Response.success()); } }
訪問:/api/test/success
返回:
{ "code": "10000", "message": "success", "data": "111111" }
訪問:/api/test/error?name=zhangsan
返回:
{ "code": "500", "message": "系統(tǒng)系統(tǒng),請聯(lián)系管理員", "data": null }
通過以上步驟,我們成功地封裝了全局異常并進行了統(tǒng)一處理。這種方式不僅提高了代碼的可維護性,還使得異常信息的格式更加一致,方便客戶端或其他系統(tǒng)進行處理。全局異常處理是一個非常值得關注的話題,尤其是在構建健壯的后端應用時。
到此這篇關于SpringBoot實現全局異常的封裝和統(tǒng)一處理的文章就介紹到這了,更多相關SpringBoot全局異常內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java 實戰(zhàn)項目錘煉之網上商城系統(tǒng)的實現流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java+jsp+servlet+mysql+ajax實現一個網上商城系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11springboot 自定義配置Boolean屬性不生效的解決
這篇文章主要介紹了springboot 自定義配置Boolean屬性不生效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03