您好,登錄后才能下訂單哦!
這篇文章主要介紹了JAVA錯誤類結果類和分頁結果類代碼詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
代碼如下
public interface ErrorCode { String getCode(); String getMsg(); /** * 公共錯誤碼<br/> * 碼段:10000~10099 * * */ public enum CommonError implements ErrorCode { SUCCESS("0", "success"), UNKNOWN_ERROR("10000", "未知錯誤"), SYSTEM_ERROR("10001", "系統內部處理異常[%s]"), REQUEST_ERROR("10002", "請求地址或參數錯誤"), DB_PROCESS_FAILED("10003", "數據庫處理失敗"), DATA_NOT_FOUND("10004", "數據不存在"), PARAM_REQUIRED("10005", "參數為空[%s]"); private String code; private String msg; private CommonError(String code, String msg) { this.code = code; this.msg = msg; } @Override public String getCode() { return this.code; } @Override public String getMsg() { return this.msg; } public ErrorCode setParams(Object... params) { this.msg = String.format(this.msg, params); return this; } } /** * 接口錯誤碼<br/> * 碼段:10100~10199 * */ public enum ApiError implements ErrorCode { REQUEST_EMPTY("10100", "請求數據為空"), API_NOT_EXISTS("10102", "接口不存在"); private String code; private String msg; private ApiError(String code, String msg) { this.code = code; this.msg = msg; } @Override public String getCode() { return this.code; } @Override public String getMsg() { return this.msg; } } }
ErrorCode error = CommonError.SYSTEM_ERROR.setParams(e.getCause().getMessage());
Result:
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; public Result() {} public Result(ErrorCode error, Object... msgParams) { this.setError(error, msgParams); } public Result(ErrorCode error) { this.setError(error); } public void setError(ErrorCode 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(ErrorCode 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); } @Override public String toString() { return "Result [result=" + result + ", msg=" + msg + ", data=" + data + "]"; } }
PageResult:
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 Integer total = 0; private List<T> rows = new ArrayList<>(); public PageResult() {} public PageResult(ErrorCode error, Object... msgParams) { this.setError(error, msgParams); } public PageResult(ErrorCode 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 Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } public void setError(ErrorCode 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(ErrorCode error) { this.setError(error, new Object[] {}); } public boolean isSuccess() { return SUCCESS.equals(result); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。