码迷,mamicode.com
首页 > 其他好文 > 详细

项目中自定义返回任意数据或者消息

时间:2018-03-31 18:14:28      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:返回   public   serial   string   build   项目   data   str   post   

public class AjaxMessage implements Serializable {

private String state;
private String msg;
private Object data;

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

public String toJson() {
return JSON.toJSONString(this);
}


public static Builder SYS_ERR() {
return new Builder(ResponseCode.SYS_ERR);
}

public static Builder SUCC() {
return new Builder(ResponseCode.SUCC);
}


public static Builder ERR(ResponseCode responseCode) {
return new Builder(responseCode);
}

public static Builder ERR(String state, String msg) {
return new Builder(state, msg);
}

public static Builder ERR(String msg) {
return new Builder("00001", msg);
}


public AjaxMessage(Builder builder) {
this.state = builder.state;
this.msg = builder.msg;
this.data = builder.data;
}

public static class Builder {

private String state;
private String msg;
private Object data;

public Builder(ResponseCode responseCode) {
this.state = responseCode.getState();
this.msg = responseCode.getMsg();
}

public Builder(String state, String msg) {
this.state = state;
this.msg = msg;
}


public Builder code(String state) {
this.state = state;
return this;
}

public Builder message(String msg) {
this.msg = msg;
return this;
}

public Builder data(Object data) {
this.data = data;
return this;
}

public AjaxMessage build() {
return new AjaxMessage(this);
}
}


}

项目中自定义返回任意数据或者消息

标签:返回   public   serial   string   build   项目   data   str   post   

原文地址:https://www.cnblogs.com/JerryTomcat/p/8683108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!