springboot的統(tǒng)一異常處理,使用@RestControllerAdvice詳解
springboot統(tǒng)一異常處理,使用@RestControllerAdvice
@RestControllerAdvice 是 Spring Boot 中用于全局異常處理的注解,它結(jié)合了 @ControllerAdvice 和 @ResponseBody 的功能。
這意味著使用 @RestControllerAdvice 注解的類將應(yīng)用于所有 @RequestMapping 方法,并且任何從這些方法返回的對象都會被轉(zhuǎn)換為 HTTP 響應(yīng)體。
下面是如何使用 @RestControllerAdvice 實(shí)現(xiàn)統(tǒng)一異常處理的一個示例:
創(chuàng)建自定義異常類
首先,創(chuàng)建一些自定義異常類來表示不同的錯誤情況。
例如:
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException(String message) {
super(message);
}
}定義API錯誤響應(yīng)格式
為了確保API返回的一致性,可以創(chuàng)建一個標(biāo)準(zhǔn)的錯誤響應(yīng)結(jié)構(gòu),如 ApiError 類:
public class ApiError {
private HttpStatus status;
private String message;
private LocalDateTime timestamp;
public ApiError(HttpStatus status, String message, Throwable throwable) {
this.status = status;
this.message = message;
this.timestamp = LocalDateTime.now();
}
// Getters and Setters...
}使用@RestControllerAdvice創(chuàng)建全局異常處理器
然后,你可以創(chuàng)建一個帶有 @RestControllerAdvice 注解的類,用來處理不同類型的異常:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ApiError> handleResourceNotFoundException(ResourceNotFoundException ex) {
ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex.getMessage(), ex);
return new ResponseEntity<>(apiError, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiError> handleAllExceptions(Exception ex) {
ApiError apiError = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, "An error occurred", ex);
return new ResponseEntity<>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
}
// 你可以添加更多特定的 @ExceptionHandler 方法來處理其他類型的異常
}配置全局異常屬性(可選)
你還可以在 application.properties 或 application.yml 文件中配置一些全局的行為,
例如是否顯示堆棧跟蹤信息:
# application.properties server.error.include-stacktrace=never
或者在 YAML 文件中:
# application.yml
server:
error:
include-stacktrace: never通過這種方式,@RestControllerAdvice 提供了一種簡潔的方法來集中處理整個應(yīng)用程序中的異常,確保所有異常都能以一致的方式響應(yīng)客戶端請求。
此外,由于它自帶了 @ResponseBody 功能,所以特別適合 RESTful Web 服務(wù)。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot中@RestControllerAdvice注解實(shí)現(xiàn)全局異常處理類
- SpringBoot項(xiàng)目中@RestControllerAdvice全局異常失效問題的解決
- SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因分析
- SpringBoot中@RestControllerAdvice注解的使用
- SpringBoot的@RestControllerAdvice作用詳解
- SpringBoot常用注解@RestControllerAdvice詳解
- SpringBoot中的@RestControllerAdvice注解詳解
- SpringBoot?@RestControllerAdvice注解對返回值統(tǒng)一封裝的處理方法
- SpringBoot中@RestControllerAdvice 全局異常處理的實(shí)現(xiàn)
相關(guān)文章
Spring Boot集成mongodb數(shù)據(jù)庫過程解析
這篇文章主要介紹了Spring Boot集成mongodb數(shù)據(jù)庫過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼
這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)數(shù)據(jù)字典的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
MybatisPlus?LambdaQueryWrapper使用int默認(rèn)值的坑及解決
這篇文章主要介紹了MybatisPlus?LambdaQueryWrapper使用int默認(rèn)值的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01
Java阻塞隊(duì)列必看類:BlockingQueue快速了解大體框架和實(shí)現(xiàn)思路
這篇文章主要介紹了Java阻塞隊(duì)列必看類:BlockingQueue快速了解大體框架和實(shí)現(xiàn)思路,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10

