@Data
public class Resp<T> {
private Integer code;
private String msg;
private T data;
//有参构造器
public Resp(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
//无参构造器
public Resp(){
}
//只放数据的构造器
public Resp(T data) {
this.data = data;
}
//成功的静态方法
public static <T>Resp<T> ok(T data){
Resp<T> resp = new Resp<T>();
resp.setCode(0);
resp.setMsg("success");
resp.setData(data);
return resp;
}
//重载成功的静态方法
public static <T> Resp<T> ok(){
Resp<T> resp = new Resp<T>();
resp.setCode(0);
resp.setMsg("success");
return resp;
}
//失败的静态方法
public static<T> Resp<T> fail(String msg){
Resp<T> resp = new Resp<T>();
resp.setCode(1);//操作失败
resp.setMsg(msg);
return resp;
}
//链式调用
public Resp<T> msg(String msg){
this.setMsg(msg);
return this;
}
public Resp<T> code(Integer code){
this.setCode(code);
return this;
}
}
常用工具——统一返回响应
最新推荐文章于 2024-08-12 21:29:36 发布