介绍
- 全局异常不满足的情况下,不想在写好多好多 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*/@RestControllerpublic 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*/@RestControllerpublic class ServiceController {@Autowiredprivate 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### 多个异常处理 - 1GET http://localhost:8111/b?i=1### 多个异常处理 - 2GET http://localhost:8111/b?i=2### 全部公用默认code - 1GET http://localhost:8111/c?i=1### 全部公用默认code - 2GET http://localhost:8111/c?i=2### 测试注解在类上 - 1GET http://localhost:8111/e### 测试注解在类上 - 2GET http://localhost:8111/f
http://localhost:8111/aHTTP/1.1 200tlogTraceId: 1540271301946658816Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Fri, 24 Jun 2022 09:51:09 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 500,"message": "错误的零","data": null,"ts": 1656064269900,"traceId": "1540271301946658816","success": false}响应文件已保存。> 2022-06-24T175110.200.jsonResponse code: 200; Time: 7ms; Content length: 108 byteshttp://localhost:8111/b?i=1HTTP/1.1 200tlogTraceId: 1540269365272924160Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Fri, 24 Jun 2022 09:43:28 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 500,"message": "错误的零","data": null,"ts": 1656063808161,"traceId": "1540269365272924160","success": false}响应文件已保存。> 2022-06-24T174328.200.jsonResponse code: 200; Time: 28ms; Content length: 108 byteshttp://localhost:8111/b?i=2HTTP/1.1 200tlogTraceId: 1540269318305107968Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Fri, 24 Jun 2022 09:43:16 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 501,"message": "下标越界","data": null,"ts": 1656063796963,"traceId": "1540269318305107968","success": false}响应文件已保存。> 2022-06-24T174317.200.jsonResponse code: 200; Time: 9ms; Content length: 108 byteshttp://localhost:8111/c?i=1HTTP/1.1 200tlogTraceId: 1540269489399156736Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Fri, 24 Jun 2022 09:43:57 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 500,"message": "错误的零","data": null,"ts": 1656063837755,"traceId": "1540269489399156736","success": false}响应文件已保存。> 2022-06-24T174359.200.jsonResponse code: 200; Time: 5ms; Content length: 108 byteshttp://localhost:8111/c?i=2HTTP/1.1 200tlogTraceId: 1540269522760650752Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Fri, 24 Jun 2022 09:44:05 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 500,"message": "下标越界","data": null,"ts": 1656063845710,"traceId": "1540269522760650752","success": false}响应文件已保存。> 2022-06-24T174405.200.jsonResponse code: 200; Time: 31ms; Content length: 108 byteshttp://localhost:8111/eHTTP/1.1 200tlogTraceId: 1543570432677502976Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Sun, 03 Jul 2022 12:20:43 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 500,"message": "错误的零","data": null,"ts": 1656850843959,"traceId": "1543570432677502976","success": false}响应文件已保存。> 2022-07-03T202044.200.jsonResponse code: 200; Time: 17ms; Content length: 108 byteshttp://localhost:8111/fHTTP/1.1 200tlogTraceId: 1543570454013927424Content-Type: application/json;charset=UTF-8Transfer-Encoding: chunkedDate: Sun, 03 Jul 2022 12:20:49 GMTKeep-Alive: timeout=60Connection: keep-alive{"code": 501,"message": "下标越界","data": null,"ts": 1656850849046,"traceId": "1543570454013927424","success": false}响应文件已保存。> 2022-07-03T202049.200.jsonResponse 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}
自定义

