/**
* 统一返回对象 封装
*
* @author SuperMudada
* @ClassName ResponseData
* @Created-Date: 2017/12/20
*/
public class ResponseData implements Serializable {
private Object data;
private String msg;
private int code;
public ResponseData(CodeEnums enums) {
this.msg = enums.getMessage();
this.code = enums.getCode();
}
public ResponseData() {
}
public ResponseData(CodeEnums enums, Object data) {
this.data = data;
this.msg = enums.getMessage();
this.code = enums.getCode();
}
public ResponseData ReturnSuccess(Object data) {
this.data = data;
this.msg = CodeEnums.SUCCESS.getMessage();
this.code = CodeEnums.SUCCESS.getCode();
return this;
}
public ResponseData ReturnSuccess() {
this.data = null;
this.msg = CodeEnums.SUCCESS.getMessage();
this.code = CodeEnums.SUCCESS.getCode();
return this;
}
public ResponseData ReturnFail(String message) {
this.data = null;
this.msg = message;
this.code = CodeEnums.INVALID.getCode();
return this;
}
public ResponseData ReturnError() {
this.data = null;
this.msg = CodeEnums.ERROR.getMessage();
this.code = CodeEnums.ERROR.getCode();
return this;
}
public Boolean ReturnIsSuccess() {
return this.code == CodeEnums.SUCCESS.getCode();
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public String toString() {
return "ResponseData{" +
"data=" + data +
", msg=‘" + msg + ‘\‘‘ +
", code=" + code +
‘}‘;
}
}
/**
* Created by oplus on 2017/6/8.
*/
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);
}
}
}