likes
comments
collection
share

jeecgboot 封装接口返回数据格式

作者站长头像
站长
· 阅读数 0

在前后端分离、微服务开发的情况下,接口返回数据格式变得非常重要。比如端程序员a有自己的一套规范,程序员b也有自己的一套规范,在这种情况下,对于程序员c来说对接就非常麻烦了;这个时候如果规定程序员a和程序员b、c都只用一套规范模板,对于对接者来说就对接就方便了许多。统一规范能够极大地降低对接难度,减少开发人员的沟通。在 jeecgboot 框架中,采用的是自己封装一个 Result 类,用于规范后端接口返回格式,接下来看看作者是怎么写的。

作者在 \jeecg-boot-master\jeecg-boot-base-core\src\main\java\org\jeecg\common\api\vo 包中创建了一个 Result.java 类,

@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

/**
 * 成功标志
 */
@ApiModelProperty(value = "成功标志")
private boolean success = true;

/**
 * 返回处理消息
 */
@ApiModelProperty(value = "返回处理消息")
private String message = "";

/**
 * 返回代码
 */
@ApiModelProperty(value = "返回代码")
private Integer code = 0;

/**
 * 返回数据对象 data
 */
@ApiModelProperty(value = "返回数据对象")
private T result;

/**
 * 时间戳
 */
@ApiModelProperty(value = "时间戳")
private long timestamp = System.currentTimeMillis();

public Result() {
}

    /**
     * 兼容VUE3版token失效不跳转登录页面
     * @param code
     * @param message
     */
public Result(Integer code, String message) {
	this.code = code;
	this.message = message;
}

public Result<T> success(String message) {
	this.message = message;
	this.code = CommonConstant.SC_OK_200;
	this.success = true;
	return this;
}

public static<T> Result<T> ok() {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	return r;
}

public static<T> Result<T> ok(String msg) {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
	r.setResult((T) msg);
	r.setMessage(msg);
	return r;
}

public static<T> Result<T> ok(T data) {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	r.setResult(data);
	return r;
}

public static<T> Result<T> OK() {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	return r;
}

/**
 * 此方法是为了兼容升级所创建
 *
 * @param msg
 * @param <T>
 * @return
 */
public static<T> Result<T> OK(String msg) {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	r.setMessage(msg);
	//Result OK(String msg)方法会造成兼容性问题 issues/I4IP3D
	r.setResult((T) msg);
	return r;
}

public static<T> Result<T> OK(T data) {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	r.setResult(data);
	return r;
}

public static<T> Result<T> OK(String msg, T data) {
	Result<T> r = new Result<T>();
	r.setSuccess(true);
	r.setCode(CommonConstant.SC_OK_200);
	r.setMessage(msg);
	r.setResult(data);
	return r;
}

public static<T> Result<T> error(String msg, T data) {
	Result<T> r = new Result<T>();
	r.setSuccess(false);
	r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
	r.setMessage(msg);
	r.setResult(data);
	return r;
}

public static<T> Result<T> error(String msg) {
	return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
}

public static<T> Result<T> error(int code, String msg) {
	Result<T> r = new Result<T>();
	r.setCode(code);
	r.setMessage(msg);
	r.setSuccess(false);
	return r;
}

public Result<T> error500(String message) {
	this.message = message;
	this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
	this.success = false;
	return this;
}

/**
 * 无权限访问返回结果
 */
public static<T> Result<T> noauth(String msg) {
	return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
}

@JsonIgnore
private String onlTable;
}

(1)首先是 @Data 和 @ApiModel 注解,这两个是 lombok 和 swagger 工具带的。

lombok 能简写类中的一些构造方法
swagger 是一个 api 文档

(2)继承 Serializable ,并且设置 private static final long serialVersionUID = 1L 序列化id,在程序版本升级时能够保持不同版本的兼容性,避免程序报出不一致的错误。

(3)定义变量,用于设值返回

success:成功标志
message:返回处理消息
code:返回代码
result:返回数据对象
timestamp:时间戳

(4)定义方法

该类中定义了很多方法,如Result()、success()、ok()、OK()、error()、error500()等一系列方法,参数不同时,返回的结果也不尽相同,根据使用时场景选择合适的即可。

通过该方法,对接的人拿到 Result 对象后,只需要通过其中的属性值,就能快速接入程序。

以上只是 jeecgboot 框架提供的一套返回格式模板,但最多只是一个参照,往往开发团队都会有自己内部的对接习惯,这种情况下完全可以自己封装一个数据格式模板。