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

Posted 今天又下下雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目中自定义返回任意数据或者消息相关的知识,希望对你有一定的参考价值。

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


}

以上是关于项目中自定义返回任意数据或者消息的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 中自定义异常返回空消息 [重复]

活动中自定义对话框内的 ANDROID 片段

laravel5.4中自定义ajax请求响应类

CXF 中自定义SOAPHeader

如何在 MSBuild 中自定义 C++ 编译器错误消息?

Django中自定义过滤器的使用