返回结果支持泛型

This commit is contained in:
e
2020-07-31 15:47:01 +08:00
parent 445c20158b
commit fbaa1a53f5
@@ -1,111 +1,138 @@
package org.jeecg.common.api.vo; package org.jeecg.common.api.vo;
import java.io.Serializable; import java.io.Serializable;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.constant.CommonConstant;
import lombok.Data; import lombok.Data;
/** /**
* 接口返回数据格式 * 接口返回数据格式
* @author scott * @author scott
* @email jeecgos@163.com * @email jeecgos@163.com
* @date 2019年1月19日 * @date 2019年1月19日
*/ */
@Data @Data
@ApiModel(value="接口返回对象", description="接口返回对象") @ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable { public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
* 成功标志 * 成功标志
*/ */
@ApiModelProperty(value = "成功标志") @ApiModelProperty(value = "成功标志")
private boolean success = true; private boolean success = true;
/** /**
* 返回处理消息 * 返回处理消息
*/ */
@ApiModelProperty(value = "返回处理消息") @ApiModelProperty(value = "返回处理消息")
private String message = "操作成功!"; private String message = "操作成功!";
/** /**
* 返回代码 * 返回代码
*/ */
@ApiModelProperty(value = "返回代码") @ApiModelProperty(value = "返回代码")
private Integer code = 0; private Integer code = 0;
/** /**
* 返回数据对象 data * 返回数据对象 data
*/ */
@ApiModelProperty(value = "返回数据对象") @ApiModelProperty(value = "返回数据对象")
private T result; private T result;
/** /**
* 时间戳 * 时间戳
*/ */
@ApiModelProperty(value = "时间戳") @ApiModelProperty(value = "时间戳")
private long timestamp = System.currentTimeMillis(); private long timestamp = System.currentTimeMillis();
public Result() { public Result() {
} }
public Result<T> success(String message) { public Result<T> success(String message) {
this.message = message; this.message = message;
this.code = CommonConstant.SC_OK_200; this.code = CommonConstant.SC_OK_200;
this.success = true; this.success = true;
return this; return this;
} }
@Deprecated
public static Result<Object> ok() { public static Result<Object> ok() {
Result<Object> r = new Result<Object>(); Result<Object> r = new Result<Object>();
r.setSuccess(true); r.setSuccess(true);
r.setCode(CommonConstant.SC_OK_200); r.setCode(CommonConstant.SC_OK_200);
r.setMessage("成功"); r.setMessage("成功");
return r; return r;
} }
public static Result<Object> ok(String msg) { @Deprecated
Result<Object> r = new Result<Object>(); public static Result<Object> ok(String msg) {
r.setSuccess(true); Result<Object> r = new Result<Object>();
r.setCode(CommonConstant.SC_OK_200); r.setSuccess(true);
r.setMessage(msg); r.setCode(CommonConstant.SC_OK_200);
return r; r.setMessage(msg);
} return r;
}
public static Result<Object> ok(Object data) {
Result<Object> r = new Result<Object>(); @Deprecated
r.setSuccess(true); public static Result<Object> ok(Object data) {
r.setCode(CommonConstant.SC_OK_200); Result<Object> r = new Result<Object>();
r.setResult(data); r.setSuccess(true);
return r; r.setCode(CommonConstant.SC_OK_200);
} r.setResult(data);
return r;
public static Result<Object> error(String msg) { }
return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
} public static<T> Result<T> OK() {
Result<T> r = new Result<T>();
public static Result<Object> error(int code, String msg) { r.setSuccess(true);
Result<Object> r = new Result<Object>(); r.setCode(CommonConstant.SC_OK_200);
r.setCode(code); r.setMessage("成功");
r.setMessage(msg); return r;
r.setSuccess(false); }
return r;
} public static<T> Result<T> OK(T data) {
Result<T> r = new Result<T>();
public Result<T> error500(String message) { r.setSuccess(true);
this.message = message; r.setCode(CommonConstant.SC_OK_200);
this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500; r.setResult(data);
this.success = false; return r;
return this; }
}
/** public static<T> Result<T> OK(String msg, T data) {
* 无权限访问返回结果 Result<T> r = new Result<T>();
*/ r.setSuccess(true);
public static Result<Object> noauth(String msg) { r.setCode(CommonConstant.SC_OK_200);
return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg); r.setMessage(msg);
} r.setResult(data);
return r;
}
public static Result<Object> error(String msg) {
return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
}
public static Result<Object> error(int code, String msg) {
Result<Object> r = new Result<Object>();
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 Result<Object> noauth(String msg) {
return error(CommonConstant.SC_JEECG_NO_AUTHZ, msg);
}
} }