统一的接口响应规范可以方便前后端开发人员的沟通。使代码更工整
1、创建消息枚举类,统一状态码定义
package com.example.goshopping.common.enums;
/**
* 统一的状态码定义
**/
public enum StatusCode {
Success(0,"成功"),
Fail(-1,"失败"),
InvalidParams(201,"非法的参数!"),
UserNamePasswordNotBlank(50000,"账户密码不能为空!"),
AccessTokenNotBlank(50001,"accessToken必填,请在请求头header中塞入该字段"),
TokenValidateExpireToken(60001,"Token已过期"),
TokenValidateCheckFail(60002,"Token验证失败"),
AccessTokenNotExist(70001,"Token不存在-请重新登录!"),
AccessTokenInvalidate(70002,"无效的Token!"),
AccessTokenNotExistRedis(80001,"Token不存在或已经过期-请重新登录!"),
AccessSessionNotExist(90001,"用户没登录或登录Session已经过期-请重新登录!"),
LoginFail(100000,"登录失败!"),
CurrUserHasNotPermission(100001,"当前用户没有权限访问该资源或者操作!"),
CurrUserNotLogin(100002,"当前用户没有登录,请先进行登录!"),
;
private Integer code;
private String msg;
StatusCode(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2、创建统一的响应数据模型
package com.example.goshopping.common.response;
import com.example.goshopping.common.enums.StatusCode;
/**
* 统一的响应数据模型
* @Date: 2019/9/9 14:27
**/
public class BaseResponse<T> {
private Integer code;
private String msg;
private T data;
public BaseResponse(StatusCode statusCode, T data) {
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
this.data = data;
}
public BaseResponse(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public BaseResponse(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public BaseResponse(StatusCode statusCode) {
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
3、controller的具体实现
@RequestMapping("/getAll")
public BaseResponse selAllUser(){
BaseResponse baseResponse=new BaseResponse(StatusCode.Success);
try {
baseResponse.setData(userService.selAllUser());
}catch (Exception e){
LOG.error(e.getMessage());
baseResponse=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
}
return baseResponse;
}
4、实现效果