1.spring系列之优雅的实现接口统一返回
2021-12-28 22:58
351 查看
好处
现在公司开发基本上都是以前后分离模式为主,所以要有个统一的数据格式,这样有什么好处呢?
- 能够提高前后端对接的效率(特别重要)
- 代码更加优雅和简洁
- 对于前端和后端维护更方便容易
实现(直接上代码)
1.状态码
这里我就初步定了两种异常状态码,更多状态码可以根据自己的情况去定义
@Getter public enum ResponseEnum { SUCCESS(0, "OK"), PARAMETER_ERROR(1,"参数异常"), SYSTEM_ERROR(500, "服务器异常,请联系管理员"); ResponseEnum(Integer code, String message) { this.code = code; this.message = message; } private final Integer code; private final String message; }
2.统一返回类
注意:这里data最好使用泛型,如果使用的object,那么swagger接口文档将无法显示对应的字段属性定义
public class ResponseModel<T> { private Integer code; private String message; private T data; public ResponseModel(Integer code, String message, T data) { this.code = code; this.message = message; this.data = data; } public static ResponseModel<Void> ok() { return ok(null); } public static <T> ResponseModel<T> ok(T data) { return new ResponseModel<>(ResponseEnum.SYSTEM_ERROR.getCode(), ResponseEnum.SYSTEM_ERROR.getMessage(), data); } public static <T> ResponseModel<T> ok(T data, String message) { return new ResponseModel<>(ResponseEnum.SYSTEM_ERROR.getCode(), message, data); } public static ResponseModel<Void> error(Integer statusCode, String message) { return new ResponseModel<>(statusCode, message, null); } public static ResponseModel<Void> error(String message) { return error(ResponseEnum.SYSTEM_ERROR.getCode(), message); } public static ResponseModel<Void> error() { return error(ResponseEnum.SYSTEM_ERROR.getCode(), ResponseEnum.SYSTEM_ERROR.getMessage()); } }
3.自定义异常
这里没啥好说的,自定义个异常继承
RuntimeException,加上状态码属性
@Getter public class BusinessException extends RuntimeException { private Integer code; public BusinessException(Integer code, String message) { super(message); this.code = code; } public BusinessException(String message) { super(message); } }
4.统一异常处理器
@ControllerAdvice @ResponseBody @Slf4j public class GlobalException { @ExceptionHandler(value = BusinessException.class) public ResponseModel<Void> BusinessExceptionError(BusinessException e) { log.error("业务异常", e); if (e.getCode() != null) { return ResponseModel.error(e.getCode(), e.getMessage()); } return ResponseModel.error(e.getMessage()); } @ExceptionHandler(value = Exception.class) public ResponseModel<Void> ExceptionError(Exception e) { log.error("系统异常", e); return ResponseModel.error(); } }
5.使用
如果不用通过返回的话,这里还需要进行异常捕获,而采用统一异常直接return即可,自定义异常直接抛出,有统一异常可以进行处理
//controller层 @ResponseBody @PostMapping("/test") public ResponseModel<Void> save() throws Exception { // 业务操作 return ResponseModel.ok(); } //service public void save(String name) throws Exception { if(name == null){ throw new BusinessException(ResponseEnum.PARAMETER_ERROR.getCode(),ResponseEnum.PARAMETER_ERROR.getMessage()); } }
6.仍存在的问题
访问服务不存在的接口404时,是无法进行捕获的,这个问题小伙伴们可以阅读我之后更新的文章会进行处理
感谢各位小伙伴阅读到最后,如有错误,敬请指正。
相关文章推荐
- spring boot 统一JSON格式的接口返回结果的实现
- spring boot架构设计——权限验证及API接口统一返回格式
- WAS应用系列:WAS6 自定义用户注册接口代码 + 企业应用安全角色配置 + WAS安全配置 实现统一灵活的集成授权
- springcloud入门系列(2)-Feign、Ribbon实现Rest接口请求和负载均衡
- Spring Cloud Feign接口返回流的实现
- spring boot通过Interceptor和HandlerMethodReturnValueHandler实现统一处理为controller返回对象统计处理时间
- Spring Boot实战之全局异常捕获 实现参数异常检查返回统一错误信息
- 解决spring boot中rest接口404 500等错误返回统一的json格式
- SpringCloud系列:利用Zuul实现统一服务网关服务,简单实现IP白名单功能
- Spring.net抛砖引玉系列(二)用接口来实现HelloWorld
- 如何设计 API 接口,实现统一格式返回?
- 解决spring boot中rest接口404,500等错误返回统一的json格式
- 【Spring Boot && Spring Cloud系列】构建Springboot项目 实现restful风格接口
- Springboot--定义Rest风格接口统一返回的数据结构
- spring boot 统一JSON格式的接口返回结果
- 如何设计 API 接口,实现统一格式返回?
- Spring Boot 2.x(六):优雅的统一返回结果
- Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群
- 枚举实现接口统一返回状态码
- 解决spring boot中rest接口404,500等错误返回统一的json格式