SpringBoot全局異常捕獲處理實(shí)現(xiàn)方案
在Spring Boot中實(shí)現(xiàn)全局異常處理可以通過(guò)以下方式:
- 使用
@ControllerAdvice注釋創(chuàng)建一個(gè)全局異常處理類(lèi),并使用@ExceptionHandler注釋來(lái)定義具體異常的處理方法。
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子類(lèi)異常
@ExceptionHandler(value = Exception.class)//作用方法上,作用:用來(lái)處理指定異常; value屬性:用來(lái)處理指定類(lèi)型的異常
@ResponseBody
public ResponseEntity<String> exceptionHeadler(Exception e){
//if(e instanceof IllegalNumberException){} 等價(jià)于 上面的方法處理指定異常
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("無(wú)效id,請(qǐng)檢查");
}
return new ResponseEntity<>("ok", HttpStatus.OK);
}
}
- Postman測(cè)試效果
通用異常:

自定義異常:

總結(jié)
在以上代碼片段中,使用了 @ExceptionHandler 注解來(lái)指定該方法會(huì)處理哪種類(lèi)型的異常。方法體中,你可以自定義返回給用戶的響應(yīng),包括HTTP狀態(tài)碼和返回信息。使用 @ControllerAdvice 注解可以確保它會(huì)接收到由控制器拋出的異常。
如果需要更多具體的自定義設(shè)置,還可以在響應(yīng)里添加 headers 信息,或者創(chuàng)建更復(fù)雜的響應(yīng)體,例如使用ResponseEntity。官方的 Spring 框架文檔提供了和這個(gè)主題相關(guān)的更多高級(jí)選項(xiàng)和最佳實(shí)踐指南。
以上就是SpringBoot全局異常捕獲處理實(shí)現(xiàn)方案的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot全局異常捕獲處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot Thymeleaf實(shí)現(xiàn)國(guó)際化的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot Thymeleaf實(shí)現(xiàn)國(guó)際化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
使用JVMTI實(shí)現(xiàn)SpringBoot的jar加密,防止反編譯
這篇文章主要介紹了使用JVMTI實(shí)現(xiàn)SpringBoot的jar加密,防止反編譯問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
JAVA基礎(chǔ)--如何通過(guò)異常處理錯(cuò)誤
這篇文章主要介紹了JAVA中如何通過(guò)異常處理錯(cuò)誤,文中講解非常細(xì)致,代碼幫助大家更好的理解,感興趣的朋友可以了解下2020-06-06
SpringBoot的HandlerInterceptor中依賴(lài)注入為null問(wèn)題
這篇文章主要介紹了SpringBoot的HandlerInterceptor中依賴(lài)注入為null問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringBoot集成Aviator實(shí)現(xiàn)參數(shù)校驗(yàn)的代碼工程
Aviator是一個(gè)高性能、輕量級(jí)的java語(yǔ)言實(shí)現(xiàn)的表達(dá)式求值引擎,主要用于各種表達(dá)式的動(dòng)態(tài)求值,本文給大家詳細(xì)介紹了SpringBoot集成Aviator實(shí)現(xiàn)參數(shù)校驗(yàn)的方法,并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-11-11
SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式
這篇文章主要介紹了SpringBoot配置文件中系統(tǒng)環(huán)境變量存在特殊字符的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Java實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類(lèi)操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類(lèi)操作,結(jié)合完整示例形式分析了Java動(dòng)態(tài)創(chuàng)建類(lèi)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-02-02
java錯(cuò)誤:?不支持發(fā)行版本?22的簡(jiǎn)單解決方法
這篇文章主要給大家介紹了關(guān)于java錯(cuò)誤:?不支持發(fā)行版本?22的簡(jiǎn)單解決方法,這個(gè)錯(cuò)誤通常是由于Java版本不兼容導(dǎo)致的,請(qǐng)檢查您的項(xiàng)目所使用的Java版本是否與您當(dāng)前安裝的Java版本一致,需要的朋友可以參考下2024-06-06

