现在好多公司都是前后端分离开发的项目,那么我们后端会统一给前端返回统一的返回值。一方面是后端代码接口返回值的统一,一方面是前端好理解及查看后端返回的值,那么接下来我将会写一下我在公司里所用到的统一返回值的代码。
主要运用了,枚举类,实体及返回前端方法类
枚举类我们可以随便定义我们的code和massage
代码纯属本人个人想法,不足之处还请大家多补充
Result实体类
package org.inlighting.handwritingjwt;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* description:返回前端实体类
* date: 2020/9/23 21:19
* @author 于嘉琪
*/
@Data
@Getter
@Setter
public class Result<T> implements Serializable {
private String returnCode;
private String message;
private T data;
public Result(String code, String msg) {
this.returnCode = code;
this.message = msg;
}
public Result(String code, String msg, T data) {
this.returnCode = code;
this.message = msg;
this.data = data;
}
}
ResultCode枚举
package org.inlighting.handwritingjwt;
/**
* description:
* date: 2020/9/23 21:26
* @author yujiaqi
*/
public enum ResultCode {
// 根据业务需要补充相应的状态码及对应的提示信息,此处作简单罗列
OK("000000", "操作成功"),
NOPARAMETER("E10002", "参数为空"),
NOAUTH("E10003", "暂无接口调用权限"),
ERROR("E10001", "系统内部异常");
private String code;
private String msg;
ResultCode(String code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 根据code值获取对应的消息
*
* @param code 状态码
* @return 消息
*/
public static String getMsgByCode(String code) {
String msg = "";
ResultCode[] resultCodes = values();
for (ResultCode resultCode : resultCodes) {
if (code.equals(resultCode.code)) {
msg = resultCode.msg;
}
}
return msg;
}
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
ResponseEntity类
package org.inlighting.handwritingjwt;
/**
* 返回前端方法类
*
* @author Yujiaqi
* @date 2020/12/15 16:30
*/
public class ResponseEntity {
public static Result success() {
return new Result(ResultCode.OK.getCode(), ResultCode.OK.getMsg());
}
public static <T> Result success(T data) {
return new Result(ResultCode.OK.getCode(), ResultCode.OK.getMsg(), data);
}
public static Result error() {
return new Result(ResultCode.ERROR.getCode(), ResultCode.ERROR.getMsg());
}
public static Result error(String code) {
return new Result(code, ResultCode.getMsgByCode(code));
}
public static Result error(String code, String msg) {
return new Result(code, msg);
}
public static <T> Result error(String code, T data) {
return new Result(code, ResultCode.getMsgByCode(code), data);
}
}
main方法测试
public static void main(String[] args) {
Result success = ResponseEntity.success();
System.out.println(success);
System.out.println("---------------------");
TestEntity testEntity = new TestEntity();
testEntity.setId(1);
testEntity.setName("测试名称");
Result success1 = ResponseEntity.success(testEntity);
System.out.println(success1);
System.out.println("---------------------");
Result error = ResponseEntity.error();
System.out.println(error);
System.out.println("---------------------");
Result e10002 = ResponseEntity.error("E10003");
System.out.println(e10002);
Result e10003 = ResponseEntity.error(ResultCode.NOAUTH.getCode());
System.out.println(e10003);
System.out.println("---------------------");
Result error1 = ResponseEntity.error("EEEEEE", "自定义返回码及返回信息");
System.out.println(error1);
Result error2 = ResponseEntity.error(ResultCode.NOAUTH.getCode(), ResultCode.NOAUTH.getMsg());
System.out.println(error2);
System.out.println("---------------------");
Result error3 = ResponseEntity.error(ResultCode.NOPARAMETER.getCode(), testEntity);
System.out.println(error3);
}
结果: