您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)java使用枚舉封裝錯(cuò)誤碼及錯(cuò)誤信息的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
使用枚舉類型來(lái)封裝project中所需要的錯(cuò)誤碼和錯(cuò)誤信息,十分方便。
package com.dear.simpler.dbservice; /** * * @author lixiang *使用枚舉類型來(lái)封裝異常碼和異常信息 * */ public enum DBServiceError { //RPC層調(diào)用錯(cuò)誤碼 DB_SERVICE_OK(20100,"服務(wù)正常") ,DB_SERVICE_DBDAO_ERROR(20104,"返回?cái)?shù)據(jù)庫(kù)的具體異常信息") ,DB_SERVICE_SPEAKER_NOT_EXISTED(20108,"數(shù)據(jù)庫(kù)中沒(méi)有要查詢的speaker") ,DB_SERVICE_SPEAKER_HAVE_NOT_VP(20109,"speaker中沒(méi)有對(duì)應(yīng)的voiceprint") ,DB_SERVICE_UNKNOWN_ERROR(20101,"未知異常") ,DB_SERVICE_AGENT_ERROR (20102, "DBServiceAgent異常") ,DB_SERVICE_NETWORK_ERROR (20103,"網(wǎng)絡(luò)異常") ,DB_SERVICE_INVALID_FUNCTION(20105, "方法名不存在") ,DB_SERVICE_INVALID_PARAMETER(20106,"方法參數(shù)錯(cuò)誤") ,DB_SERVICE_FUNCTION_NO_ACCESS(20107,"對(duì)此方法無(wú)訪問(wèn)權(quán)限"); private String msg; private int code; private DBServiceError(int code,String msg) { this.code=code; this.msg=msg; } public String getMsg() { return this.msg; } public int getCode() { return this.code; } }
使用時(shí):
DBServiceError.DB_SERVICE_NETWORK_ERROR.getCode();
DBServiceError.DB_SERVICE_NETWORK_ERROR.getMsg();
/** * 枚舉錯(cuò)誤碼 */ public emum ErrorCode{ FAILED(601,"操作失敗"), AUTH_ERROR(401,"認(rèn)證失敗"), SYS_ERROR(500,"系統(tǒng)錯(cuò)誤"), PARAM_ERROR(400,"參數(shù)錯(cuò)誤"), UNKNOWN_ERROR(499,"未知錯(cuò)誤"); private int code; private String message; private ErrorCode(int code, String message) { this.code=code; this.message=message; } public String getMessage() { return this.message; } public int getCode() { return this.code; } }
/** * 返回結(jié)果類 */ public class JsonResult { private int status = 0; private String msg = "success"; private Object data; // 幾個(gè)常用方法 public JsonResult success() { return new JsonResult(); } public JsonResult success(Object data) { return new JsonResult(data); } public JsonResult error(ErrorCode errorCode) { return new JsonResult(errorCode.getCode(), errorCode.getMessage()); } public JsonResult error(int state, String message) { return new JsonResult(state, message); } public JsonResult error(int state, String message, Object data) { return new JsonResult(state, message, data); } // 幾個(gè)構(gòu)造方法 public JsonResult() { } public JsonResult(Object data) { this.data = data; } public JsonResult(Integer state, String message) { this.state = state; this.message = message; } public JsonResult(Integer state, String message, Object data) { this.state = state; this.message = message; this.data = data; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } 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; } }
感謝各位的閱讀!關(guān)于“java使用枚舉封裝錯(cuò)誤碼及錯(cuò)誤信息的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。