spring boot mvc 的全局异常捕获,依赖下注解即可
<dependency>
<groupId>cn.tannn.jdevelops</groupId>
<artifactId>jdevelops-apis-exception</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
🥱接口包裹类 - 常规返回
{
"code": 501,
"message": "/ by zero",
"ts": 1715243347618,
"data": null,
"success": false
}
🥱接口包裹类 - 重置默认包裹类
# 关闭全局异常处理 - 默认值为true
jdevelops.exception.enabled=false
# 异常栈是否打印(默认为true打印)
jdevelops.exception.log-input=true
# 是否将自定的异常code放到:response status 中去(默认不放)
jdevelops.exception.http-servlet-response-status=true
# 设置异常信息返回的 header 种的 content type 一般不使用,用默认的就行了
# jdevelops.exception.http-servlet-response-header-content-type=
- 异常继承 BusinessException 才能自定义返回的code,否则只会使用默认值
501
/**
* jwt异常
*
* @author tnnn
* @version V1.0
* @date 2022-12-28 09:33
*/
public class TokenException extends BusinessException{
public TokenException(int code, String message) {
super(code, message);
}
public TokenException(String message) {
super(message);
}
public TokenException(String message, Throwable cause) {
super(message, cause);
}
public TokenException(ExceptionCode constantEnum) {
super(constantEnum.getCode(), constantEnum.getMessage());
}
}
- 如果不继承 **BusinessException **但是想用到自定义的code就必须使用如下方法
specialMessage
构造一个code <=====> message
的 message字符串我会在ExceptionResultWrap._result_
中做解析。
/**
* 接口幂等异常
*
* @author tnnn
* @version V1.0
* @date 2022-11-17 15:02
*/
public class IdempotentException extends RuntimeException{
/**
* ResultCode.API_DOUBLE_CALL
*/
public static int IDEMPOTENT_CODE = 602;
public static String SYMBOL = "<=====>";
public IdempotentException() {
super();
}
/**
* 特的message
* <ps>用于无法使用BusinessException,当时想自定义失败code的情况(默认是501)</ps>
* @param message message
* @return code+ ExceptionResultWrap.symbol+message; (eg. 200<=====>你错了)
*/
public static IdempotentException specialMessage(String message){
return new IdempotentException(IDEMPOTENT_CODE + SYMBOL + message);
}
}
- BusinessException
最大的内置异常,所有新建异常都必须继承他
- DataConflictException
数据冲突异常
- LoginLimitException
登录限制异常
- TokenException
token相关异常
- UserException
用户相关异常
下面处理不到的异常都会走这个进行返回给前端
我们自定义的异常
404 相关的异常 -> 路径不存在,请检查路径是否正确
空指针异常 -> 暂时无法获取数据
请求方式不对 - get post
参数错误 -> 请求参数格式错误,请检查。错误消息(消息不可读)
参数验证异常 -> javax.validation
运行时异常
参数验证异常 -> springframework.validation
- 有些错误的抛出需要进行自行捕捉,写烦了就用了AOP进行了捕捉,比如数据重复抛出的异常就可以用这个抛出个message叫数据已存在请勿重复添加
- 异常格式
@DisposeException(messages ="错误的零",
codes = 500,
exceptions = ArithmeticException.class )
public ResultVO<String> test1(){
int zero = 1 / 0;
return ResultVO.success();
}
/**
* 测试
*
* @author tnnn
* @version V1.0
* @date 2022-07-03 19:36
*/
@Service
@DisposeException(messages ={"错误的零","下标越界"}, codes = {500,501}, exceptions = {
ArithmeticException.class,
ArrayIndexOutOfBoundsException.class} )
public class IService {
/**
* 单个异常
* @return
*/
public ResultVO<String> test1(){
int zero = 1 / 0;
return ResultVO.success();
}
/**
* 多个异常处理
* @return
*/
public ResultVO<String> test2(){
int[] indexs = {1,2,3};
System.out.println(indexs[4]);
return ResultVO.success();
}
}
| 异常名
| 作用
| 备注
| | --- | --- | --- | | SQLIntegrityConstraintViolationException
| 数据重复(存在唯一索引)
| 属于 DataIntegrityViolationException 异常里的一个case
| | MysqlDataTruncation
| **列的值超出范围 **
| 属于 DataIntegrityViolationException 异常里的一个case
| | ClassNotFoundException
| 没有找到类
| | | IOException
| IO异常
| | | ArrayIndexOutOfBoundsException
| 下标越界
| | | NullPointerException
| 空指针异常
| |
### 单个异常
GET http://localhost:8111/a
### 多个异常处理 - 1
GET http://localhost:8111/b?i=1
### 多个异常处理 - 2
GET http://localhost:8111/b?i=2
### 全部公用默认code - 1
GET http://localhost:8111/c?i=1
### 全部公用默认code - 2
GET http://localhost:8111/c?i=2
### 测试注解在类上 - 1
GET http://localhost:8111/e
### 测试注解在类上 - 2
GET http://localhost:8111/f
http://localhost:8111/a
HTTP/1.1 200
tlogTraceId: 1540271301946658816
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 24 Jun 2022 09:51:09 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 500,
"message": "错误的零",
"data": null,
"ts": 1656064269900,
"traceId": "1540271301946658816",
"success": false
}
响应文件已保存。
> 2022-06-24T175110.200.json
Response code: 200; Time: 7ms; Content length: 108 bytes
http://localhost:8111/b?i=1
HTTP/1.1 200
tlogTraceId: 1540269365272924160
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 24 Jun 2022 09:43:28 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 500,
"message": "错误的零",
"data": null,
"ts": 1656063808161,
"traceId": "1540269365272924160",
"success": false
}
响应文件已保存。
> 2022-06-24T174328.200.json
Response code: 200; Time: 28ms; Content length: 108 bytes
http://localhost:8111/b?i=2
HTTP/1.1 200
tlogTraceId: 1540269318305107968
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 24 Jun 2022 09:43:16 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 501,
"message": "下标越界",
"data": null,
"ts": 1656063796963,
"traceId": "1540269318305107968",
"success": false
}
响应文件已保存。
> 2022-06-24T174317.200.json
Response code: 200; Time: 9ms; Content length: 108 bytes
http://localhost:8111/c?i=1
HTTP/1.1 200
tlogTraceId: 1540269489399156736
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 24 Jun 2022 09:43:57 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 500,
"message": "错误的零",
"data": null,
"ts": 1656063837755,
"traceId": "1540269489399156736",
"success": false
}
响应文件已保存。
> 2022-06-24T174359.200.json
Response code: 200; Time: 5ms; Content length: 108 bytes
http://localhost:8111/c?i=2
HTTP/1.1 200
tlogTraceId: 1540269522760650752
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 24 Jun 2022 09:44:05 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 500,
"message": "下标越界",
"data": null,
"ts": 1656063845710,
"traceId": "1540269522760650752",
"success": false
}
响应文件已保存。
> 2022-06-24T174405.200.json
Response code: 200; Time: 31ms; Content length: 108 bytes
http://localhost:8111/e
HTTP/1.1 200
tlogTraceId: 1543570432677502976
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 03 Jul 2022 12:20:43 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 500,
"message": "错误的零",
"data": null,
"ts": 1656850843959,
"traceId": "1543570432677502976",
"success": false
}
响应文件已保存。
> 2022-07-03T202044.200.json
Response code: 200; Time: 17ms; Content length: 108 bytes
http://localhost:8111/f
HTTP/1.1 200
tlogTraceId: 1543570454013927424
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 03 Jul 2022 12:20:49 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"code": 501,
"message": "下标越界",
"data": null,
"ts": 1656850849046,
"traceId": "1543570454013927424",
"success": false
}
响应文件已保存。
> 2022-07-03T202049.200.json
Response code: 200; Time: 16ms; Content length: 108 bytes