介绍
- 全局异常不满足的情况下,不想在写好多好多 try 了
- 全局异常时在组件里写的,所以项目中不方便添加和修改
引入依赖
2.0.5 开始支持
<dependency>
<groupId>cn.jdevelops</groupId>
<artifactId>aops-exception</artifactId>
<version>2.0.6</version>
</dependency>
<!--2.0.6开始需要自己引入apis-exception,或者使用自己的自定义全局异常处理AopException-->
<dependency>
<groupId>cn.jdevelops</groupId>
<artifactId>apis-exception</artifactId>
<version>2.0.6</version>
</dependency>
使用
由于使用的是aop,所以目前只能用在web项目上
作用在方法上
方法>类
package org.example;
import cn.jdevelops.exception.annotation.DisposeException;
import cn.jdevelops.result.result.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tnnn
* @version V1.0
* @date 2022-06-24 17:23
*/
@RestController
public class Controller {
/**
* 单个异常
* @return
*/
@GetMapping("/a")
@DisposeException(messages ="错误的零", codes = 500, exceptions = ArithmeticException.class )
public ResultVO<String> test1(){
int zero = 1 / 0;
return ResultVO.success();
}
/**
* 多个异常处理
* @param i
* @return
*/
@GetMapping("/b")
@DisposeException(messages ={"错误的零","下标越界"}, codes = {500,501}, exceptions = {
ArithmeticException.class,
ArrayIndexOutOfBoundsException.class} )
public ResultVO<String> test2(int i){
if(i == 1){
int zero = 1 / 0;
}else {
int[] indexs = {1,2,3};
System.out.println(indexs[4]);
}
return ResultVO.success();
}
/**
* 全部公用默认code
* @param i
* @return
*/
@GetMapping("/c")
@DisposeException(messages ={"错误的零","下标越界"}, exceptions = {
ArithmeticException.class,
ArrayIndexOutOfBoundsException.class} )
public ResultVO<String> test3(int i){
if(i == 1){
int zero = 1 / 0;
}else {
int[] indexs = {1,2,3};
System.out.println(indexs[4]);
}
return ResultVO.success();
}
}
可作用在类上
service
package org.example;
import cn.jdevelops.exception.annotation.DisposeException;
import cn.jdevelops.result.result.ResultVO;
import org.springframework.stereotype.Service;
/**
* 测试
*
* @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();
}
}
controller
package org.example;
import cn.jdevelops.exception.annotation.DisposeException;
import cn.jdevelops.result.result.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试
*
* @author tnnn
* @version V1.0
* @date 2022-07-03 19:37
*/
@RestController
public class ServiceController {
@Autowired
private IService iService;
/**
* 测试类上使用
* @return
*/
@GetMapping("/e")
public ResultVO<String> test1(){
iService.test1();
return ResultVO.success();
}
/**
* 测试类上使用
* @return
*/
@GetMapping("/f")
public ResultVO<String> test2(){
iService.test2();
return ResultVO.success();
}
}
效果
### 单个异常
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
异常说明
异常名 | 作用 | 备注 |
---|---|---|
SQLIntegrityConstraintViolationException |
数据重复(存在唯一索引) | 属于 DataIntegrityViolationException 异常里的一个case |
MysqlDataTruncation |
列的值超出范围 | 属于 DataIntegrityViolationException 异常里的一个case |
ClassNotFoundException |
没有找到类 | |
IOException |
IO异常 | |
ArrayIndexOutOfBoundsException |
下标越界 | |
NullPointerException |
空指针异常 |
示例项目地址
https://github.com/en-o/Jdevelops-Example/tree/main/aop-exception/http
特别提示
自定义返回的数据格式
默认的是
apis-result
定义的
{
"code": 500,
"message": "错误的零",
"data": null,
"ts": 1656063837755,
"traceId": "1540269489399156736",
"success": false
}
自定义