目录
1. 响应数据的格式
1.1 状态码划分
1.3 响应数据的封装
2. 统一数据返回
1. 响应数据的格式
在分离的环境中,我们前后交互就显得尤为重要。前端按照接口文档中的URL地址和参数要求发送请求,接着后端对业务进行处理就响应约定格式的数据,这种约定的格式常见如下:
{
"code": 200,
"message": "Success !",
"data":{}
}
1.1 状态码划分
当然这个 code 各自有不同的规范,可以参考HTTP的状态码进行区间划分
/**
200 - 00 请求成功,已经正常处理完毕
301 - 00 请求永久重定向,转移到其它URL
302 - 00 请求临时重定向
304 - 00 请求被重定向到客户端本地缓存
---数据校验---
400 - 00 客户端请求存在语法错误
400 - 01 客户端请求参数校验异常
400 - 02 用户不存在,请注册
400 - 03 用户被锁定 ,请联系管理员
400 - 04 用户或密码不正确 ,请重新登录
401 - 01 token为空,请重新登录
401 - 02 token校验失败,请重新登录
401 - 03 该账号被锁定,请联系系统管理员
401 - 04 该账号已被删除,请联系系统管理员
401 - 05 token失效,请刷新token
---无权限---
403 - 01 客户端的请求被服务器拒绝,一般为客户端没有访问权限
---服务器异常---
500 - 00 服务端永久错误
*/
1.2 状态码的封装
我们常用枚举来进行状态码及其提示的统一封装,便于管理
public enum BaseResponseCode{
SUCCESS(200,"请求成功!"),
;
BaseResponseCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 响应状态码
*/
private final int code;
/**
* 响应提示
*/
private final String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
1.3 响应数据的封装
将数据统一封装成响应数据,简洁明了
package com.example.demo.util;
import com.example.demo.exception.code.BaseResponseCode;
import com.example.demo.exception.code.ResponseCodeInterface;
import io.swagger.annotations.ApiModelProperty;
/**
* @ClassName DataResult
* @Description TODO
* @Author LiuY
* @Date 2020/12/5 16:45
*/
public class DataResult<T> {
@ApiModelProperty("响应状态码")
private int code;
@ApiModelProperty("响应提示")
private String msg;
@ApiModelProperty("响应数据")
private T data;
public DataResult(int code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public DataResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
public DataResult(ResponseCodeInterface responseCodeinterface){
this.code=responseCodeinterface.getCode();
this.msg=responseCodeinterface.getMsg();
this.data=null;
}
public DataResult(ResponseCodeInterface responseCodeinterface,T data){
this.code=responseCodeinterface.getCode();
this.msg=responseCodeinterface.getMsg();
this.data=data;
}
/**
* 操作成功,data为null
*/
public DataResult(){
this.code= BaseResponseCode.SUCCESS.getCode();
this.msg=BaseResponseCode.SUCCESS.getMsg();
this.data=null;
}
/**
* 请求成功,返回data
* @param data
*/
public DataResult(T data){
this.code=BaseResponseCode.SUCCESS.getCode();
this.msg=BaseResponseCode.SUCCESS.getMsg();
this.data=data;
}
/**
* 自定义返回,无需data
* @param code 响应状态码
* @param msg 响应提示语
* @return
*/
public static DataResult getResult(int code, String msg){
return new DataResult(code,msg);
}
/**
* 自定义data返回
* @param code 响应状态码
* @param msg 响应提示语
* @param data 响应数据
* @param <T>
* @return DataResult
*/
public static <T> DataResult getResult(int code, String msg, T data){
return new DataResult(code,msg,data);
}
/**
* 自定义返回,一般返回异常时调用,data为null
* @param baseResponseCode
* @return
*/
public static DataResult getResult(BaseResponseCode baseResponseCode){
return new DataResult(baseResponseCode);
}
/**
* 自定义返回,一般返回异常时调用
* @param baseResponseCode
* @param data
* @param <T>
* @return
*/
public static <T> DataResult getResult(BaseResponseCode baseResponseCode,T data){
return new DataResult(baseResponseCode,data);
}
/**
* 请求成功时调用,data为null
* @return
*/
public static DataResult success(){
return new DataResult();
}
/**
* 请求成功,需要返回data
* @param data
* @param <T>
* @return
*/
public static <T> DataResult success(T data){
return new DataResult(data);
}
}
2. 统一数据返回
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户模块接口")
@Slf4j
public class UserController {
@Autowired
UserService userService;
@PostMapping("/login")
@ApiOperation("用户登录接口")
public DataResult<LoginRespVO> login(@Valid @RequestBody LoginReqVO vo){
return DataResult.success(userService.login(vo));
}
}
{"code":"20000","msg":"成功","data":null}
{"code":"20000","msg":"成功","data":{"name":null,"age":0}}
{"code":"50000","msg":"服务端错误","data":null}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)