经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
统一返回对象封装和统一异常捕获封装springboot starter
来源:cnblogs  作者:Java夜未眠  时间:2023/1/18 8:43:01  对本文有异议

好久没有更新文章了,高龄开发没什么技术,去了外包公司后没怎么更新文章了。今天分享下统一处理starter,相信开发web系统的时候都是会涉及到前后端的交互,而后端返回数据的时候一般都会统一封装一个返回对象和统一处理异常,一般情况下都是在controller的每个方法中调用封装的对象,把相应的数据塞到data字段,然后返回给前端。而异常处理则是抛出某个业务异常,然后利用spring切面进行拦截处理。每个项目都需要做这些重复的动作,所以我把这个处理封装成了starter,下面介绍已下这个starter的使用,最后给出git库供大家学习交流。

添加依赖

添加统一处理依赖

  1. <dependency>
  2. <groupId>io.gitee.javalaoniu</groupId>
  3. <artifactId>jud-springboot-starter</artifactId>
  4. <version>0.0.1</version>
  5. </dependency>

启用统一处理

添加 @EnableUnifiedDisposal 注解

  1. import io.gitee.javalaoniu.jud.annotation.EnableUnifiedDisposal;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @EnableUnifiedDisposal
  5. @SpringBootApplication
  6. public class JudDemoApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(JudDemoApplication.class, args);
  9. }
  10. }

拦截的处理

像平常一样返回数据即可,不需要做其它

  1. import io.gitee.javalaoniu.jud.annotation.IgnoreResponseAdvice;
  2. import io.gitee.javalaoniu.jud.common.Result;
  3. import io.gitee.javalaoniu.jud.exception.BusinessException;
  4. import io.gitee.javalaoniu.jud.exception.ExceptionCode;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. @RestController
  10. public class DemoController {
  11. @GetMapping("test1")
  12. public String stringTest() {
  13. return "hello";
  14. // {"code":200,"data":"hello","succ":true,"ts":1673943672244}
  15. }
  16. @GetMapping("test2")
  17. public String stringNullTest() {
  18. return null;
  19. // {"code":200,"data":"","succ":true,"ts":1673943691844}
  20. }
  21. @GetMapping("test3")
  22. public Object objectEntityTest() {
  23. DemoEntity demoEntity = new DemoEntity();
  24. demoEntity.setName("张三");
  25. demoEntity.setAge(50);
  26. demoEntity.setSex(false);
  27. demoEntity.setSalary(4500000001542.26);
  28. return demoEntity;
  29. // {"succ":true,"ts":1673943709119,"data":{"name":"张三","age":50,"sex":false,"salary":4.50000000154226E12},"code":200,"msg":null}
  30. }
  31. @GetMapping("test4")
  32. public Object objectNotNullTest() {
  33. return "hello Object";
  34. // {"code":200,"data":"hello Object","succ":true,"ts":1673943726435}
  35. }
  36. @GetMapping("test5")
  37. public Object objectNullTest() {
  38. return null;
  39. // 啥也没返回,但是如果配置了json转换器的话会返回:{"code":200,"data":null,"succ":true,"ts":1673943726435}
  40. }
  41. @GetMapping("test6")
  42. public List<DemoEntity> listTest() {
  43. DemoEntity demoEntity2 = new DemoEntity();
  44. demoEntity2.setName("张三");
  45. demoEntity2.setAge(50);
  46. demoEntity2.setSex(false);
  47. demoEntity2.setSalary(4500000001542.26);
  48. DemoEntity demoEntity = new DemoEntity();
  49. demoEntity.setName("张三");
  50. demoEntity.setAge(50);
  51. demoEntity.setSex(false);
  52. demoEntity.setSalary(4500000001542.26);
  53. List<DemoEntity> list = new ArrayList<>();
  54. list.add(demoEntity);
  55. list.add(demoEntity2);
  56. return list;
  57. // {"succ":true,"ts":1673943797079,"data":[{"name":"张三","age":50,"sex":false,"salary":4.50000000154226E12},{"name":"张三","age":50,"sex":false,"salary":4.50000000154226E12}],"code":200,"msg":null}
  58. }
  59. @GetMapping("test7")
  60. public List<String> listNullTest() {
  61. return null;
  62. // {"succ":true,"ts":1673943819382,"data":null,"code":200,"msg":null}
  63. }
  64. @GetMapping("test8")
  65. public Result resultTest() {
  66. DemoEntity demoEntity = new DemoEntity();
  67. demoEntity.setName("张三");
  68. demoEntity.setAge(50);
  69. demoEntity.setSex(false);
  70. demoEntity.setSalary(4500000001542.2656564545);
  71. return Result.success(demoEntity);
  72. // {"succ":true,"ts":1673943832081,"data":{"name":"张三","age":50,"sex":false,"salary":4.500000001542266E12},"code":200,"msg":null}
  73. }
  74. @IgnoreResponseAdvice
  75. @GetMapping("test9")
  76. public String ignoreResponseTest() {
  77. return "IgnoreResponseAdvice";
  78. // IgnoreResponseAdvice
  79. }
  80. @GetMapping("test10")
  81. public String businessExceptionTest() {
  82. throw new BusinessException(ExceptionCode.EXCEPTION);
  83. // {"succ":false,"ts":1673943862588,"data":null,"code":500,"msg":"服务器开小差,请稍后再试(Internal Server Error)"}
  84. }
  85. }

不拦截处理

对不需要统一处理的controller或者方法使用下面注解

  1. @IgnoreResponseAdvice
  2. @GetMapping("test9")
  3. public String ignoreResponseTest() {
  4. // 在方法上使用,直接返回IgnoreResponseAdvice字符串给前端
  5. return "IgnoreResponseAdvice";
  6. }

可以看到,使用统一处理starter后,统一返回对象和统一异常处理不需要自己在处理,非常方便。
git仓库地址:https://gitee.com/javalaoniu/javalaoniu-jud

原文链接:https://www.cnblogs.com/liangxianning/p/17058195.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号