这篇文章主要介绍“SpringBoot怎么实现统一后端返回格式”,在日常操作中,相信很多人在SpringBoot怎么实现统一后端返回格式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”SpringBoot怎么实现统一后端返回格式”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1.为什么要对SpringBoot返回统一的标准格式
在默认情况下,SpringBoot的返回格式常见的有三种:
1.1 返回String
@GetMapping("/hello")public String hello() { return "hello";}
此时调用接口获取到的返回值是这样:
hello
1.2 返回自定义对象
@GetMapping("/student")public Student getStudent() { Student student = new Student(); student.setId(1); student.setName("didiplus"); return student;}//student的类@Datapublic class Student { private Integer id; private String name;}
此时调用接口获取到的返回值是这样:
{"id":1,"name":"didiplus"}
1.3 接口异常
@GetMapping("/error")public int error(){ int i = 9/0; return i;}
此时调用接口获取到的返回值是这样
SpringBoot的版本是v2.6.7,
2.定义返回对象
package com.didiplus.common.web.response;import lombok.Data;import java.io.Serializable;@Datapublic class Result<T> implements Serializable { private boolean success; private int code; private String msg; private T data; private long timestamp ; public Result (){ this.timestamp = System.currentTimeMillis(); } public static <T> Result<T> success() { return success(null); } public static <T> Result<T> success(T data){ return success(ResultCode.RC100.getMessage(),data); } public static <T> Result<T> success(String message) { return success(message, null); } public static <T> Result<T> success(String message, T data) { return success(ResultCode.RC100.getCode(), message, data); } public static <T> Result<T> success(int code, String message) { return success(code, message, null); } public static <T> Result<T> success(int code,String message,T data) { Result<T> result = new Result<T>(); result.setCode(code); result.setMsg(message); result.setSuccess(true); result.setData(data); return result; } public static <T> Result<T> failure() { return failure(ResultCode.RC100.getMessage()); } public static <T> Result<T> failure(String message) { return failure(message, null); } public static <T> Result<T> failure(String message, T data) { return failure(ResultCode.RC999.getCode(), message, data); } public static <T> Result<T> failure(int code, String message) { return failure(ResultCode.RC999.getCode(), message, null); } public static <T> Result<T> failure(int code, String message, T data) { Result<T> result = new Result<T>(); result.setCode(code); result.setMsg(message); result.setSuccess(false); result.setData(data); return result; } public static <T> Result<T> decide(boolean b) { return decide(b, ResultCode.RC100.getMessage(), ResultCode.RC999.getMessage()); } public static <T> Result<T> decide(boolean b, String success, String failure) { if (b) { return success(success); } else { return failure(failure); } }}
3.定义状态码
package com.didiplus.common.web.response;import lombok.Getter;public enum ResultCode { RC100(100,"操作成功"), RC999(999,"操作失败"), RC200(200,"服务开启限流保护,请稍后再试!"), RC201(201,"服务开启降级保护,请稍后再试!"), RC202(202,"热点参数限流,请稍后再试!"), RC203(203,"系统规则不满足要求,请稍后再试!"), RC204(204,"授权规则不通过,请稍后再试!"), RC403(403,"无访问权限,请联系管理员授予权限"), RC401(401,"匿名用户访问无权限资源时的异常"), RC500(500,"系统异常,请稍后重试"), INVALID_TOKEN(2001,"访问令牌不合法"), ACCESS_DENIED(2003,"没有权限访问该资源"), CLIENT_AUTHENTICATION_FAILED(1001,"客户端认证失败"), USERNAME_OR_PASSWORD_ERROR(1002,"用户名或密码错误"), UNSUPPORTED_GRANT_TYPE(1003, "不支持的认证模式"); @Getter private final int code; @Getter private final String message; ResultCode(int code, String message) { this.code = code; this.message = message; }}
4.统一返回格式
@GetMapping("/hello") public Result<String> hello() { return Result.success("操作成功","hello"); }
此时调用接口获取到的返回值是这样:
{"success":true,"code":100,"msg":"操作成功","data":"hello","timestamp":1650785058049}
这样确实已经实现了我们想要的结果,我在很多项目中看到的都是这种写法,在Controller层通过Result.success()对返回结果进行包装后返回给前端。这样显得不够专业而且不够优雅。 所以呢我们需要对代码进行优化,目标就是不要每个接口都手工制定Result返回值。
5.高级实现方式
要优化这段代码很简单,我们只需要借助SpringBoot提供的ResponseBodyAdvice即可。
5.1 ResponseBodyAdvice的源码
public interface ResponseBodyAdvice<T> { boolean supports(MethodParameter var1, Class<? extends HttpMessageConverter<?>> var2); @Nullable T beforeBodyWrite(@Nullable T var1, MethodParameter var2, MediaType var3, Class<? extends HttpMessageConverter<?>> var4, ServerHttpRequest var5, ServerHttpResponse var6);}
只需要编写一个具体实现类即可
@RestControllerAdvicepublic class ResponseAdvice implements ResponseBodyAdvice<Object> { @Autowired ObjectMapper objectMapper; @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return true; } @SneakyThrows @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { if (body instanceof String){ return objectMapper.writeValueAsString(Result.success(ResultCode.RC100.getMessage(),body)); } return Result.success(ResultCode.RC100.getMessage(),body); }}
需要注意两个地方:
@RestControllerAdvice注解 @RestControllerAdvice是@RestController注解的增强,可以实现三个方面的功能:
全局异常处理
全局数据绑定
全局数据预处理
5.2 String类型判断
if (body instanceof String){ return objectMapper.writeValueAsString(Result.success(ResultCode.RC100.getMessage(),body)); }
这段代码一定要加,如果Controller直接返回String的话,SpringBoot是直接返回,故我们需要手动转换成json。 经过上面的处理我们就再也不需要通过ResultData.success()来进行转换了,直接返回原始数据格式,SpringBoot自动帮我们实现包装类的封装。
@GetMapping("/hello") public String hello() { return "hello,didiplus"; } @GetMapping("/student") public Student getStudent() { Student student = new Student(); student.setId(1); student.setName("didiplus"); return student; }
此时我们调用接口返回的数据结果为:
{ "success": true, "code": 100, "msg": "操作成功", "data": "hello,didiplus", "timestamp": 1650786993454 }
到此,关于“SpringBoot怎么实现统一后端返回格式”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!