import lombok.Data;
import java.io.Serializable;
/**
* 后端统一返回结果
* @param <T>
*/
@Data
public class Result<T> implements Serializable {
private String status;
private Integer code; //编码:1成功,0和其它数字为失败
private String info; //错误信息
private T data; //数据
public static <T> Result<T> success() {
Result<T> result = new Result<T>();
result.status = "success";
result.info = "请求成功";
result.code = 200;
return result;
}
public static <T> Result<T> success(T object) {
Result<T> result = new Result<T>();
result.status = "success";
result.info = "请求成功";
result.data = object;
result.code = 200;
return result;
}
public static <T> Result<T> error(String info) {
Result result = new Result();
result.status = "error";
result.info = info;
result.code = 0;
return result;
}
}
导入lombok依赖即可,前后端存数据和取数据都是在属性 data中。
此模板并非定死,可以根据前端的规则进行相应属性名的修改,及根据业务的实际情况添加或修改其中的属性。