JAVA错误类结果类和分页结果类代码详解
这篇文章主要介绍了JAVA错误类结果类和分页结果类代码详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
代码如下
publicinterfaceErrorCode{ StringgetCode(); StringgetMsg(); /** *公共错误码
*码段:10000~10099 * * */ publicenumCommonErrorimplementsErrorCode{ 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]"); privateStringcode; privateStringmsg; privateCommonError(Stringcode,Stringmsg){ this.code=code; this.msg=msg; } @Override publicStringgetCode(){ returnthis.code; } @Override publicStringgetMsg(){ returnthis.msg; } publicErrorCodesetParams(Object...params){ this.msg=String.format(this.msg,params); returnthis; } } /** *接口错误码
*码段:10100~10199 * */ publicenumApiErrorimplementsErrorCode{ REQUEST_EMPTY("10100","请求数据为空"), API_NOT_EXISTS("10102","接口不存在"); privateStringcode; privateStringmsg; privateApiError(Stringcode,Stringmsg){ this.code=code; this.msg=msg; } @Override publicStringgetCode(){ returnthis.code; } @Override publicStringgetMsg(){ returnthis.msg; } } }
ErrorCodeerror=CommonError.SYSTEM_ERROR.setParams(e.getCause().getMessage());
Result:
publicclassResult{ publicstaticfinalStringSUCCESS="0"; publicstaticfinalStringSCUCESS_MSG="success"; privateStringresult=SUCCESS; privateStringmsg=SCUCESS_MSG; privateTdata; publicResult(){} publicResult(ErrorCodeerror,Object...msgParams){ this.setError(error,msgParams); } publicResult(ErrorCodeerror){ this.setError(error); } publicvoidsetError(ErrorCodeerror,Object...msgParams){ this.result=error.getCode(); this.msg=error.getMsg(); if(msgParams!=null&&msgParams.length>0){ this.msg=String.format(msg,msgParams); } } publicvoidsetError(ErrorCodeerror){ this.setError(error,newObject[]{}); } publicStringgetResult(){ returnresult; } publicvoidsetResult(Stringresult){ this.result=result; } publicStringgetMsg(){ returnmsg; } publicvoidsetMsg(Stringmsg){ this.msg=msg; } publicTgetData(){ returndata; } publicvoidsetData(Tdata){ this.data=data; } publicbooleanisSuccess(){ returnSUCCESS.equals(result); } @Override publicStringtoString(){ return"Result[result="+result+",msg="+msg+",data="+data+"]"; } }
PageResult:
publicclassPageResult{ publicstaticfinalStringSUCCESS="0"; publicstaticfinalStringSCUCESS_MSG="success"; privateStringresult=SUCCESS; privateStringmsg=SCUCESS_MSG; privateIntegertotal=0; privateList rows=newArrayList<>(); publicPageResult(){} publicPageResult(ErrorCodeerror,Object...msgParams){ this.setError(error,msgParams); } publicPageResult(ErrorCodeerror){ this.setError(error); } publicStringgetResult(){ returnresult; } publicvoidsetResult(Stringresult){ this.result=result; } publicStringgetMsg(){ returnmsg; } publicvoidsetMsg(Stringmsg){ this.msg=msg; } publicIntegergetTotal(){ returntotal; } publicvoidsetTotal(Integertotal){ this.total=total; } publicList getRows(){ returnrows; } publicvoidsetRows(List rows){ this.rows=rows; } publicvoidsetError(ErrorCodeerror,Object...msgParams){ this.result=error.getCode(); this.msg=error.getMsg(); if(msgParams!=null&&msgParams.length>0){ this.msg=String.format(msg,msgParams); } } publicvoidsetError(ErrorCodeerror){ this.setError(error,newObject[]{}); } publicbooleanisSuccess(){ returnSUCCESS.equals(result); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。