網站首頁 編程語言 正文
@ControllerAdvice和、@ExceptionHandler可以構建全局異常處理器。
它們的底層原理是使用Spring AOP方式對被@RequestMaping聲明的方法進行增強,從而達到異常統一處理的目的。
這樣做的好處的就是不需要我們手動的對每一個異常進行try…catch。
首先@ControllerAdvice相當于通知類,同時又要具備切點表達式的身份,它會對所有被@RequestMaping聲明的方法進行增強。
而@ExceptionHandler聲明的方法相當于通知(增強方法)但它更加強大,它會對切入點進行上下文檢索其聲明的異常(@ExceptionHandler(value = Exception.class))所在,進行try…catch統一對異常進行處理。
全局異常處理流程:
1、創建自定義業務異常類;
2、創建一個類并聲明@ControllerAdvice,作為全局異常處理類
3、在全局異常處理類上,創建增強方法,聲明@ExceptionHandler檢索需要處理的異常。
自定業務義異常類:
/**
* 自定義業務異常類
*/
public class CustomException extends Exception {
public CustomException(String message){
//拋出異常時自定義異常信息替代RuntimeException提供的message
super(message);
}
}
全局異常處理類:
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
@ResponseBody
public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex) {
String message = ex.getMessage();
if (message.contains("Duplicate entry")) {
String[] split = message.split(" ");
return R.error(split[2] + "已存在");
}
return R.error("未知錯誤");
}
@ExceptionHandler(CustomException.class)
@ResponseBody
public R<String> customExceptionHandler(CustomException ce) {
return R.error(ce.getMessage());
}
}
測試:
比如這里有一個業務異常需要處理,分類關聯菜品,不能刪除:
@Override
public R<String> delete(Long id) {
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
//判斷當前分類是否關聯了菜品
if (dishMapper.selectCount(dishLambdaQueryWrapper) > 0) {
throw new CustomException("當前分類下關聯了菜品,不能刪除");
}
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
//判斷當前分類是否關聯了套餐
if (setmealMapper.selectCount(setmealLambdaQueryWrapper) > 0) {
throw new CustomException("當前分類下關聯了套餐,不能刪除");
}
categoryMapper.deleteById(id);
return R.success("刪除分類成功");
}
最終執行流程:
增強后的代碼相當于:
@Override
public R<String> delete(Long id) {
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
//判斷當前分類是否關聯了菜品
if (dishMapper.selectCount(dishLambdaQueryWrapper) > 0) {
throw new CustomException("當前分類下關聯了菜品,不能刪除");
}
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
//判斷當前分類是否關聯了套餐
if (setmealMapper.selectCount(setmealLambdaQueryWrapper) > 0) {
try {
throw new CustomException("當前分類下關聯了套餐,不能刪除");
} catch (CustomException e) {
return R.error(e.getMessage());
}
}
categoryMapper.deleteById(id);
return R.success("刪除分類成功");
}
原文鏈接:https://blog.csdn.net/weixin_41786712/article/details/125919089
相關推薦
- 2022-08-26 Pandas?DataFrame.drop()刪除數據的方法實例_python
- 2022-08-15 Docker常見用法
- 2022-12-05 go?code?review?代碼調試_Golang
- 2023-02-05 詳解Golang中Context的三個常見應用場景_Golang
- 2022-07-18 CSS基礎語法和盒模型
- 2022-09-05 Redis的數據刪除策略
- 2022-03-29 帶你了解C++中vector的用法_C 語言
- 2023-07-24 navigator里包含tabBar內容時失效
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支