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

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);
}
}


}
posted @ 2018-03-31 17:21  今天又下小雨  阅读(177)  评论(0编辑  收藏  举报