這篇文章主要介紹了JAVA錯誤類結(jié)果類和分頁結(jié)果類代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
創(chuàng)新新互聯(lián),憑借十余年的網(wǎng)站設(shè)計制作、做網(wǎng)站經(jīng)驗,本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計網(wǎng)站有上1000+案例。做網(wǎng)站建設(shè),選創(chuàng)新互聯(lián)公司。
代碼如下
public interface ErrorCode { String getCode(); String getMsg(); /** * 公共錯誤碼<br/> * 碼段:10000~10099 * * */ public enum CommonError implements ErrorCode { SUCCESS("0", "success"), UNKNOWN_ERROR("10000", "未知錯誤"), SYSTEM_ERROR("10001", "系統(tǒng)內(nèi)部處理異常[%s]"), REQUEST_ERROR("10002", "請求地址或參數(shù)錯誤"), DB_PROCESS_FAILED("10003", "數(shù)據(jù)庫處理失敗"), DATA_NOT_FOUND("10004", "數(shù)據(jù)不存在"), PARAM_REQUIRED("10005", "參數(shù)為空[%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", "請求數(shù)據(jù)為空"), 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); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
新聞標(biāo)題:JAVA錯誤類結(jié)果類和分頁結(jié)果類代碼詳解
標(biāo)題URL:http://jinyejixie.com/article36/ggecsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、域名注冊、網(wǎng)站維護(hù)、、自適應(yīng)網(wǎng)站、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)