import org.apache.commons.lang.StringUtils; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class Result extends BaseDomain implements Serializable { private String code; private String msg; private T data; private Result() { this.code = UnifyStrErrorCode.SUCCESS.getCode(); this.msg = UnifyStrErrorCode.SUCCESS.getMsg(); } private Result(T data) { this.code = UnifyStrErrorCode.SUCCESS.getCode(); this.msg = UnifyStrErrorCode.SUCCESS.getMsg(); this.setData(data); } private Result(String code, String msg) { this.code = UnifyStrErrorCode.SUCCESS.getCode(); this.msg = UnifyStrErrorCode.SUCCESS.getMsg(); this.setCode(code); this.setMsg(msg); } private Result(String code, String msg, T data) { this.code = UnifyStrErrorCode.SUCCESS.getCode(); this.msg = UnifyStrErrorCode.SUCCESS.getMsg(); this.setCode(code); this.setMsg(msg); this.setData(data); } public Result setError(String code, String msg) { this.setCode(code); this.setMsg(msg); return this; } public Result setError(UnifyStrErrorCode errorCode) { this.setCode(errorCode.getCode()); this.setMsg(errorCode.getMsg()); return this; } public boolean isSuccess() { return StringUtils.equals(this.getCode(), UnifyStrErrorCode.SUCCESS.getCode()); } public static Result instance() { return new Result(); } public static Result instance(T data) { return new Result(data); } public static Result instance(String code, String msg) { return new Result(code, msg); } public static Result instance(String code, String msg, T data) { return new Result(code, msg, data); } public String getCode() { return this.code; } public void setCode(String code) { this.code = code; } public String getMsg() { return this.msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return this.data; } public void setData(T data) { this.data = data; } public Map toJsonMap(){ Map map = new HashMap<>(); map.put("data",this.data); map.put("msg",this.msg); map.put("code",this.code); return map; } } 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
import org.apache.commons.lang.builder.ToStringBuilder; import java.io.Serializable; public class BaseDomain implements Serializable { public BaseDomain() { } public String toString() { return ToStringBuilder.reflectionToString(this); } }