SpringBoot全局異常捕獲處理實現(xiàn)方案
在Spring Boot中實現(xiàn)全局異常處理可以通過以下方式:
- 使用
@ControllerAdvice
注釋創(chuàng)建一個全局異常處理類,并使用@ExceptionHandler
注釋來定義具體異常的處理方法。
import your.package.IllegalNumberException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * @class GlobalExceptionHeadler * @date 2021/8/28 13:41 */ @ControllerAdvice public class GlobalExceptionHeadler { //處理指定異常 @ExceptionHandler(value = IllegalNumberException.class)//IllegalNumberException 自定義異常 @ResponseBody public ResponseEntity<String> illegalNumberExceptionHeadler(Exception e){ System.out.println("進(jìn)入非法參數(shù)異常處理"); return new ResponseEntity<>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR); } //處理exceptioin子類異常 @ExceptionHandler(value = Exception.class)//作用方法上,作用:用來處理指定異常; value屬性:用來處理指定類型的異常 @ResponseBody public ResponseEntity<String> exceptionHeadler(Exception e){ //if(e instanceof IllegalNumberException){} 等價于 上面的方法處理指定異常 System.out.println("進(jìn)入自定義異常處理"); return new ResponseEntity<>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR); } }
- 自定義異常
/** * @class IllegalNumberException * @date 2021/8/28 14:05 */ public class IllegalNumberException extends RuntimeException{ public IllegalNumberException(String message){ super(message); } }
- Controller
import your.package.IllegalNumberException; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j public class Test2Controller { @GetMapping("/testException") public ResponseEntity<String> demo(){ int i = 1/0; return new ResponseEntity<>("ok", HttpStatus.OK); } @GetMapping("/testIllegalNumberException/{id}") public ResponseEntity<String> demo(@PathVariable("id") Integer id){ if(id < 0){ throw new IllegalNumberException("無效id,請檢查"); } return new ResponseEntity<>("ok", HttpStatus.OK); } }
- Postman測試效果
通用異常:
自定義異常:
總結(jié)
在以上代碼片段中,使用了 @ExceptionHandler 注解來指定該方法會處理哪種類型的異常。方法體中,你可以自定義返回給用戶的響應(yīng),包括HTTP狀態(tài)碼和返回信息。使用 @ControllerAdvice 注解可以確保它會接收到由控制器拋出的異常。
如果需要更多具體的自定義設(shè)置,還可以在響應(yīng)里添加 headers 信息,或者創(chuàng)建更復(fù)雜的響應(yīng)體,例如使用ResponseEntity。官方的 Spring 框架文檔提供了和這個主題相關(guān)的更多高級選項和最佳實踐指南。
以上就是SpringBoot全局異常捕獲處理實現(xiàn)方案的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot全局異常捕獲處理的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot Thymeleaf實現(xiàn)國際化的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot Thymeleaf實現(xiàn)國際化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10使用JVMTI實現(xiàn)SpringBoot的jar加密,防止反編譯
這篇文章主要介紹了使用JVMTI實現(xiàn)SpringBoot的jar加密,防止反編譯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08SpringBoot的HandlerInterceptor中依賴注入為null問題
這篇文章主要介紹了SpringBoot的HandlerInterceptor中依賴注入為null問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot集成Aviator實現(xiàn)參數(shù)校驗的代碼工程
Aviator是一個高性能、輕量級的java語言實現(xiàn)的表達(dá)式求值引擎,主要用于各種表達(dá)式的動態(tài)求值,本文給大家詳細(xì)介紹了SpringBoot集成Aviator實現(xiàn)參數(shù)校驗的方法,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-11-11SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式
這篇文章主要介紹了SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Java實現(xiàn)動態(tài)創(chuàng)建類操作示例
這篇文章主要介紹了Java實現(xiàn)動態(tài)創(chuàng)建類操作,結(jié)合完整示例形式分析了Java動態(tài)創(chuàng)建類的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-02-02