Result:
import java.util.HashMap; import java.util.Map; import com.door.common.constant.IErrorCode; /** * * 结果类 date: 2018年1月17日 上午9:52:08 * * @author why * @version @param <T> * @since JDK 1.7 * @see */ public class Result<T> { public static final String SUCCESS = "0"; public static final String SCUCESS_MSG = "success"; private String result = SUCCESS; private String msg = SCUCESS_MSG; private T data; private Map<String, Object> header = new HashMap<>(); public Result() {} public Result(IErrorCode error, Object... msgParams) { this.setError(error, msgParams); } public Result(IErrorCode error) { this.setError(error); } public void setError(IErrorCode error, Object... msgParams) { this.result = error.getCode(); this.msg = error.getMsg(); if (msgParams != null && msgParams.length > 0) { this.msg = String.format(msg, msgParams); } } public void setError(IErrorCode error) { this.setError(error, new Object[] {}); } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } public boolean isSuccess() { return SUCCESS.equals(result); } public void addHeader(String key, String value) { header.put(key, value); } public Map<String, Object> getHeader() { return header; } public void setHeader(Map<String, Object> header) { this.header = header; } @Override public String toString() { return "Result [result=" + result + ", msg=" + msg + ", data=" + data + "]"; } }
PageResult:
import java.util.ArrayList; import java.util.List; import com.door.common.constant.IErrorCode; /** * * 分页result date: 2018年1月17日 上午9:51:50 * * @author why * @version @param <T> * @since JDK 1.7 * @see */ public class PageResult<T> { public static final String SUCCESS = "0"; public static final String SCUCESS_MSG = "success"; private String result = SUCCESS; private String msg = SCUCESS_MSG; private Long total = 0L; private List<T> rows = new ArrayList<>(); public PageResult() {} public PageResult(IErrorCode error, Object... msgParams) { this.setError(error, msgParams); } public PageResult(IErrorCode error) { this.setError(error); } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } public void setError(IErrorCode error, Object... msgParams) { this.result = error.getCode(); this.msg = error.getMsg(); if (msgParams != null && msgParams.length > 0) { this.msg = String.format(msg, msgParams); } } public void setError(IErrorCode error) { this.setError(error, new Object[] {}); } public boolean isSuccess() { return SUCCESS.equals(result); } }