介绍

  • 全局异常不满足的情况下,不想在写好多好多 try 了
    • 全局异常时在组件里写的,所以项目中不方便添加和修改
  • image.png

引入依赖

2.0.5 开始支持

  1. <dependency>
  2. <groupId>cn.jdevelops</groupId>
  3. <artifactId>aops-exception</artifactId>
  4. <version>2.0.6</version>
  5. </dependency>
  6. <!--2.0.6开始需要自己引入apis-exception,或者使用自己的自定义全局异常处理AopException-->
  7. <dependency>
  8. <groupId>cn.jdevelops</groupId>
  9. <artifactId>apis-exception</artifactId>
  10. <version>2.0.6</version>
  11. </dependency>

使用

由于使用的是aop,所以目前只能用在web项目上

作用在方法上

方法>类

  1. package org.example;
  2. import cn.jdevelops.exception.annotation.DisposeException;
  3. import cn.jdevelops.result.result.ResultVO;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @author tnnn
  9. * @version V1.0
  10. * @date 2022-06-24 17:23
  11. */
  12. @RestController
  13. public class Controller {
  14. /**
  15. * 单个异常
  16. * @return
  17. */
  18. @GetMapping("/a")
  19. @DisposeException(messages ="错误的零", codes = 500, exceptions = ArithmeticException.class )
  20. public ResultVO<String> test1(){
  21. int zero = 1 / 0;
  22. return ResultVO.success();
  23. }
  24. /**
  25. * 多个异常处理
  26. * @param i
  27. * @return
  28. */
  29. @GetMapping("/b")
  30. @DisposeException(messages ={"错误的零","下标越界"}, codes = {500,501}, exceptions = {
  31. ArithmeticException.class,
  32. ArrayIndexOutOfBoundsException.class} )
  33. public ResultVO<String> test2(int i){
  34. if(i == 1){
  35. int zero = 1 / 0;
  36. }else {
  37. int[] indexs = {1,2,3};
  38. System.out.println(indexs[4]);
  39. }
  40. return ResultVO.success();
  41. }
  42. /**
  43. * 全部公用默认code
  44. * @param i
  45. * @return
  46. */
  47. @GetMapping("/c")
  48. @DisposeException(messages ={"错误的零","下标越界"}, exceptions = {
  49. ArithmeticException.class,
  50. ArrayIndexOutOfBoundsException.class} )
  51. public ResultVO<String> test3(int i){
  52. if(i == 1){
  53. int zero = 1 / 0;
  54. }else {
  55. int[] indexs = {1,2,3};
  56. System.out.println(indexs[4]);
  57. }
  58. return ResultVO.success();
  59. }
  60. }

可作用在类上

service

  1. package org.example;
  2. import cn.jdevelops.exception.annotation.DisposeException;
  3. import cn.jdevelops.result.result.ResultVO;
  4. import org.springframework.stereotype.Service;
  5. /**
  6. * 测试
  7. *
  8. * @author tnnn
  9. * @version V1.0
  10. * @date 2022-07-03 19:36
  11. */
  12. @Service
  13. @DisposeException(messages ={"错误的零","下标越界"}, codes = {500,501}, exceptions = {
  14. ArithmeticException.class,
  15. ArrayIndexOutOfBoundsException.class} )
  16. public class IService {
  17. /**
  18. * 单个异常
  19. * @return
  20. */
  21. public ResultVO<String> test1(){
  22. int zero = 1 / 0;
  23. return ResultVO.success();
  24. }
  25. /**
  26. * 多个异常处理
  27. * @return
  28. */
  29. public ResultVO<String> test2(){
  30. int[] indexs = {1,2,3};
  31. System.out.println(indexs[4]);
  32. return ResultVO.success();
  33. }
  34. }

controller

  1. package org.example;
  2. import cn.jdevelops.exception.annotation.DisposeException;
  3. import cn.jdevelops.result.result.ResultVO;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * 测试
  9. *
  10. * @author tnnn
  11. * @version V1.0
  12. * @date 2022-07-03 19:37
  13. */
  14. @RestController
  15. public class ServiceController {
  16. @Autowired
  17. private IService iService;
  18. /**
  19. * 测试类上使用
  20. * @return
  21. */
  22. @GetMapping("/e")
  23. public ResultVO<String> test1(){
  24. iService.test1();
  25. return ResultVO.success();
  26. }
  27. /**
  28. * 测试类上使用
  29. * @return
  30. */
  31. @GetMapping("/f")
  32. public ResultVO<String> test2(){
  33. iService.test2();
  34. return ResultVO.success();
  35. }
  36. }

效果

image.png image.png

  1. ### 单个异常
  2. GET http://localhost:8111/a
  3. ### 多个异常处理 - 1
  4. GET http://localhost:8111/b?i=1
  5. ### 多个异常处理 - 2
  6. GET http://localhost:8111/b?i=2
  7. ### 全部公用默认code - 1
  8. GET http://localhost:8111/c?i=1
  9. ### 全部公用默认code - 2
  10. GET http://localhost:8111/c?i=2
  11. ### 测试注解在类上 - 1
  12. GET http://localhost:8111/e
  13. ### 测试注解在类上 - 2
  14. GET http://localhost:8111/f
  1. http://localhost:8111/a
  2. HTTP/1.1 200
  3. tlogTraceId: 1540271301946658816
  4. Content-Type: application/json;charset=UTF-8
  5. Transfer-Encoding: chunked
  6. Date: Fri, 24 Jun 2022 09:51:09 GMT
  7. Keep-Alive: timeout=60
  8. Connection: keep-alive
  9. {
  10. "code": 500,
  11. "message": "错误的零",
  12. "data": null,
  13. "ts": 1656064269900,
  14. "traceId": "1540271301946658816",
  15. "success": false
  16. }
  17. 响应文件已保存。
  18. > 2022-06-24T175110.200.json
  19. Response code: 200; Time: 7ms; Content length: 108 bytes
  20. http://localhost:8111/b?i=1
  21. HTTP/1.1 200
  22. tlogTraceId: 1540269365272924160
  23. Content-Type: application/json;charset=UTF-8
  24. Transfer-Encoding: chunked
  25. Date: Fri, 24 Jun 2022 09:43:28 GMT
  26. Keep-Alive: timeout=60
  27. Connection: keep-alive
  28. {
  29. "code": 500,
  30. "message": "错误的零",
  31. "data": null,
  32. "ts": 1656063808161,
  33. "traceId": "1540269365272924160",
  34. "success": false
  35. }
  36. 响应文件已保存。
  37. > 2022-06-24T174328.200.json
  38. Response code: 200; Time: 28ms; Content length: 108 bytes
  39. http://localhost:8111/b?i=2
  40. HTTP/1.1 200
  41. tlogTraceId: 1540269318305107968
  42. Content-Type: application/json;charset=UTF-8
  43. Transfer-Encoding: chunked
  44. Date: Fri, 24 Jun 2022 09:43:16 GMT
  45. Keep-Alive: timeout=60
  46. Connection: keep-alive
  47. {
  48. "code": 501,
  49. "message": "下标越界",
  50. "data": null,
  51. "ts": 1656063796963,
  52. "traceId": "1540269318305107968",
  53. "success": false
  54. }
  55. 响应文件已保存。
  56. > 2022-06-24T174317.200.json
  57. Response code: 200; Time: 9ms; Content length: 108 bytes
  58. http://localhost:8111/c?i=1
  59. HTTP/1.1 200
  60. tlogTraceId: 1540269489399156736
  61. Content-Type: application/json;charset=UTF-8
  62. Transfer-Encoding: chunked
  63. Date: Fri, 24 Jun 2022 09:43:57 GMT
  64. Keep-Alive: timeout=60
  65. Connection: keep-alive
  66. {
  67. "code": 500,
  68. "message": "错误的零",
  69. "data": null,
  70. "ts": 1656063837755,
  71. "traceId": "1540269489399156736",
  72. "success": false
  73. }
  74. 响应文件已保存。
  75. > 2022-06-24T174359.200.json
  76. Response code: 200; Time: 5ms; Content length: 108 bytes
  77. http://localhost:8111/c?i=2
  78. HTTP/1.1 200
  79. tlogTraceId: 1540269522760650752
  80. Content-Type: application/json;charset=UTF-8
  81. Transfer-Encoding: chunked
  82. Date: Fri, 24 Jun 2022 09:44:05 GMT
  83. Keep-Alive: timeout=60
  84. Connection: keep-alive
  85. {
  86. "code": 500,
  87. "message": "下标越界",
  88. "data": null,
  89. "ts": 1656063845710,
  90. "traceId": "1540269522760650752",
  91. "success": false
  92. }
  93. 响应文件已保存。
  94. > 2022-06-24T174405.200.json
  95. Response code: 200; Time: 31ms; Content length: 108 bytes
  96. http://localhost:8111/e
  97. HTTP/1.1 200
  98. tlogTraceId: 1543570432677502976
  99. Content-Type: application/json;charset=UTF-8
  100. Transfer-Encoding: chunked
  101. Date: Sun, 03 Jul 2022 12:20:43 GMT
  102. Keep-Alive: timeout=60
  103. Connection: keep-alive
  104. {
  105. "code": 500,
  106. "message": "错误的零",
  107. "data": null,
  108. "ts": 1656850843959,
  109. "traceId": "1543570432677502976",
  110. "success": false
  111. }
  112. 响应文件已保存。
  113. > 2022-07-03T202044.200.json
  114. Response code: 200; Time: 17ms; Content length: 108 bytes
  115. http://localhost:8111/f
  116. HTTP/1.1 200
  117. tlogTraceId: 1543570454013927424
  118. Content-Type: application/json;charset=UTF-8
  119. Transfer-Encoding: chunked
  120. Date: Sun, 03 Jul 2022 12:20:49 GMT
  121. Keep-Alive: timeout=60
  122. Connection: keep-alive
  123. {
  124. "code": 501,
  125. "message": "下标越界",
  126. "data": null,
  127. "ts": 1656850849046,
  128. "traceId": "1543570454013927424",
  129. "success": false
  130. }
  131. 响应文件已保存。
  132. > 2022-07-03T202049.200.json
  133. 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 定义的

  1. {
  2. "code": 500,
  3. "message": "错误的零",
  4. "data": null,
  5. "ts": 1656063837755,
  6. "traceId": "1540269489399156736",
  7. "success": false
  8. }

自定义

全局异常捕获