统一返回对象封装和统一异常捕获封装springboot starter
2024-10-20 16:07:00
好久没有更新文章了,高龄开发没什么技术,去了外包公司后没怎么更新文章了。今天分享下统一处理starter,相信开发web系统的时候都是会涉及到前后端的交互,而后端返回数据的时候一般都会统一封装一个返回对象和统一处理异常,一般情况下都是在controller的每个方法中调用封装的对象,把相应的数据塞到data字段,然后返回给前端。而异常处理则是抛出某个业务异常,然后利用spring切面进行拦截处理。每个项目都需要做这些重复的动作,所以我把这个处理封装成了starter,下面介绍已下这个starter的使用,最后给出git库供大家学习交流。
添加依赖
添加统一处理依赖
<dependency>
<groupId>io.gitee.javalaoniu</groupId>
<artifactId>jud-springboot-starter</artifactId>
<version>0.0.1</version>
</dependency>
启用统一处理
添加 @EnableUnifiedDisposal 注解
import io.gitee.javalaoniu.jud.annotation.EnableUnifiedDisposal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableUnifiedDisposal
@SpringBootApplication
public class JudDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JudDemoApplication.class, args);
}
}
拦截的处理
像平常一样返回数据即可,不需要做其它
import io.gitee.javalaoniu.jud.annotation.IgnoreResponseAdvice;
import io.gitee.javalaoniu.jud.common.Result;
import io.gitee.javalaoniu.jud.exception.BusinessException;
import io.gitee.javalaoniu.jud.exception.ExceptionCode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class DemoController {
@GetMapping("test1")
public String stringTest() {
return "hello";
// {"code":200,"data":"hello","succ":true,"ts":1673943672244}
}
@GetMapping("test2")
public String stringNullTest() {
return null;
// {"code":200,"data":"","succ":true,"ts":1673943691844}
}
@GetMapping("test3")
public Object objectEntityTest() {
DemoEntity demoEntity = new DemoEntity();
demoEntity.setName("张三");
demoEntity.setAge(50);
demoEntity.setSex(false);
demoEntity.setSalary(4500000001542.26);
return demoEntity;
// {"succ":true,"ts":1673943709119,"data":{"name":"张三","age":50,"sex":false,"salary":4.50000000154226E12},"code":200,"msg":null}
}
@GetMapping("test4")
public Object objectNotNullTest() {
return "hello Object";
// {"code":200,"data":"hello Object","succ":true,"ts":1673943726435}
}
@GetMapping("test5")
public Object objectNullTest() {
return null;
// 啥也没返回,但是如果配置了json转换器的话会返回:{"code":200,"data":null,"succ":true,"ts":1673943726435}
}
@GetMapping("test6")
public List<DemoEntity> listTest() {
DemoEntity demoEntity2 = new DemoEntity();
demoEntity2.setName("张三");
demoEntity2.setAge(50);
demoEntity2.setSex(false);
demoEntity2.setSalary(4500000001542.26);
DemoEntity demoEntity = new DemoEntity();
demoEntity.setName("张三");
demoEntity.setAge(50);
demoEntity.setSex(false);
demoEntity.setSalary(4500000001542.26);
List<DemoEntity> list = new ArrayList<>();
list.add(demoEntity);
list.add(demoEntity2);
return list;
// {"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}
}
@GetMapping("test7")
public List<String> listNullTest() {
return null;
// {"succ":true,"ts":1673943819382,"data":null,"code":200,"msg":null}
}
@GetMapping("test8")
public Result resultTest() {
DemoEntity demoEntity = new DemoEntity();
demoEntity.setName("张三");
demoEntity.setAge(50);
demoEntity.setSex(false);
demoEntity.setSalary(4500000001542.2656564545);
return Result.success(demoEntity);
// {"succ":true,"ts":1673943832081,"data":{"name":"张三","age":50,"sex":false,"salary":4.500000001542266E12},"code":200,"msg":null}
}
@IgnoreResponseAdvice
@GetMapping("test9")
public String ignoreResponseTest() {
return "IgnoreResponseAdvice";
// IgnoreResponseAdvice
}
@GetMapping("test10")
public String businessExceptionTest() {
throw new BusinessException(ExceptionCode.EXCEPTION);
// {"succ":false,"ts":1673943862588,"data":null,"code":500,"msg":"服务器开小差,请稍后再试(Internal Server Error)"}
}
}
不拦截处理
对不需要统一处理的controller或者方法使用下面注解
@IgnoreResponseAdvice
@GetMapping("test9")
public String ignoreResponseTest() {
// 在方法上使用,直接返回IgnoreResponseAdvice字符串给前端
return "IgnoreResponseAdvice";
}
可以看到,使用统一处理starter后,统一返回对象和统一异常处理不需要自己在处理,非常方便。
git仓库地址:https://gitee.com/javalaoniu/javalaoniu-jud
最新文章
- 【Linux】lsof 命令,记一次端口占用查询
- IaaS/PaaS/SaaS
- 为什么GOF的23种设计模式里面没有MVC?
- [整理]详记被忽略的Get与Post
- java之yield(),sleep(),wait()区别详解-备忘笔记
- linux ubuntu 思源黑体安装
- DataTable转换List<;T>;集合的方法
- WPF Radio组的绑定
- Keil C51处理可重入函数问题的探讨
- Linux中iptables设置详细
- high performance program (SSE4.2 intrin instruction)
- 自己整理的openresty安装步骤
- jq 获取下一个兄弟原素 下拉箭头旋转
- C++ Error C2664:无法将参数 1 从“const char [9]”转换为“LPCWSTR”解决方案
- js动态获取select选中的option
- Python+Pandas 读取Oracle数据库
- Jmeter接口测试实例图文示例
- eclipse 注销和取消注销
- Android-HttpsURLConnectionHelp工具类
- SPDY以及HTTP2.0
热门文章
- 三、Kubernetes调度
- 成熟企业级开源监控解决方案Zabbix6.2关键功能实战-上
- 你不知道的React Developer Tools,20 分钟带你掌握 9 个 React 组件调试技巧
- java学习之Cookie与Session
- ironic组件硬件自检服务——ironic-inspector
- htaccess如何配置隐藏index.php文件
- Thinkphp6使用腾讯云发送短信步骤
- 简单的sql注入3
- JavaScript合集(流程控制语句)
- 当我们的执行 java -jar xxx.jar 的时候底层到底做了什么?