统一异常处理中心
[👉 源码](https://github.com/geniusay/Assistant)
<dependency>
<groupId>io.github.geniusay</groupId>
<artifactId>assistant-boot-start</artifactId>
<version>1.1.1</version>
</dependency>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 统一异常处理中心
简述
对于SpringBoot的全局异常稍做了一些封装和开关设置,导入assistant-service依赖默认开启使用
# 已有异常处理器
异常处理器 | 描述 |
---|---|
commonExceptionHandler | 基本异常处理类,负责拦截Exception异常,也就是最后一项的拦截 |
springWebExceptionHandler | SpringWeb接口异常处理类,一般处理Http请求响应异常 |
validateExceptionHandler | 参数校验异常处理类,一般处理参数校验异常 |
每异常处理器的启动由AssistantExceptionHandlerCondition控制中心控制,他会检测被注解的拦截器类上的AssistantControllerAdvice注解中的value是否在配置文件的disable中有展示,如果有则会禁用对应的拦截器
# 导入依赖
注意
请确保项目中引入了assistant-boot-starter依赖
<dependency>
<groupId>io.github.geniusay</groupId>
<artifactId>assistant-service</artifactId>
<version>尽量boot-starter版本同步</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 关闭异常处理类
在配置文件中关闭你所需要的异常处理类即可,关闭多个用逗号分隔
assistant:
exception:
handler:
disables: springWebExceptionHandler,validateExceptionHandler
1
2
3
4
2
3
4
# 创建异常处理类
1,使用@AssistantControllerAdvice注解,并写入名字
@
AssistantControllerAdvice("geniusExceptionHandler")2,加上Conditional被对应的控制器控制
@Conditional(AssistantExceptionHandlerCondition.class)`
3,其他按照正常@ControllerAdvice的流程编写即可
帮助我们改善此页面! (opens new window)
上次更新: 2024/06/22, 16:36:11