Java封装统一的Result Model案例
在开发过程中,有时候会需要使用错误码+错误信息的形式,来返回某些业务操作的错误结果信息,来代替效率较低的异常传递。
这样就需要封装一个统一的Resultmodel作为返回值,代替直接返回数据等结果。
1.定义错误码-错误信息接口
错误码可能是String、Integer、Long等类型,也可能是enum类型。因此这里使用泛型来代替,错误码类型,可以提高灵活性
。也可以通过枚举直接实现CodeMessage接口。
publicinterfaceCodeMessage{ CgetCode(); StringgetMessage(); }
2.实现ResultModel
这里定义的是service层的Result。有时候在controller层只会作一些简单的参数校验,在service层会作进一步的校验,
如果controller中需要统一返回一个JsonResult可以将ServiceResult作一个简单的转换器进行转换。
publicclassServiceResultimplementsSerializable{ publicstaticfinalCodeMessage SUCCESS=newDefaultMessage<>("00000000","success"); privateTdata; privateCodeMessage message; privatebooleanisSuccess; ServiceResult(Tdata,booleanisSuccess,CodeMessage message){ this.data=data; this.message=message; this.isSuccess=isSuccess; } publicTgetData(){ returndata; } publicCodeMessage getCodeMessage(){ returnmessage; } publicbooleanisSuccess(){ returnisSuccess; } publicstatic ServiceResultBuilder success(){ ServiceResultBuilder builder=builder(); returnbuilder.isSuccess(true); } publicstatic ServiceResultBuilder success(CodeMessage codeMessage){ ServiceResultBuilder builder=builder(); returnbuilder.isSuccess(true).code(codeMessage.getCode()).message(codeMessage.getMessage()); } publicstatic ServiceResult success(Ddata){ ServiceResultBuilder success=success(SUCCESS); returnsuccess.data(data).build(); } publicstatic ServiceResult error(CodeMessage codeMessage){ ServiceResultBuilder builder=builder(); returnbuilder.isSuccess(false).code(codeMessage.getCode()).message(codeMessage.getMessage()).build(); } publicstatic ServiceResultBuilder error(){ ServiceResultBuilder builder=builder(); returnbuilder.isSuccess(false); } static ServiceResultBuilder builder(){ returnnewServiceResultBuilder<>(); } publicstaticclassDefaultMessage implementsCodeMessage ,java.io.Serializable{ privateCcode; privateStringmessage; publicDefaultMessage(Ccode,Stringmessage){ this.code=code; this.message=message; } @Override publicCgetCode(){ returncode; } @Override publicStringgetMessage(){ returnmessage; } @Override publicStringtoString(){ return"DefaultMessage{"+ "code="+code+ ",message='"+message+'\''+ '}'; } } publicstaticclassServiceResultBuilder { privateTdata; privateCcode; privateStringmessage; privatebooleanisSuccess; ServiceResultBuilder(){//packageprivate } publicServiceResultBuilder data(Tdata){ this.data=data; returnthis; } ServiceResultBuilder isSuccess(booleanisSuccess){ this.isSuccess=isSuccess; returnthis; } publicServiceResultBuilder code(Ccode){ this.code=code; returnthis; } publicServiceResultBuilder message(Stringmessage){ this.message=message; returnthis; } publicServiceResult build(){ Objects.requireNonNull(code,"code"); Objects.requireNonNull(code,"message"); returnnewServiceResult<>(data,isSuccess,newDefaultMessage<>(code,message)); } } @Override publicStringtoString(){ return"ServiceResult{"+ "data="+data+ ",message="+message+ ",isSuccess="+isSuccess+ '}'; } }
3.使用
定义枚举错误码
pubilcenumCodeMessageEnumimplementsCodeMessage{ some_error(1001,"错误信息。"); Integercode; Stringmessage; CodeMessageEnum(Integercode,Stringmessage){ this.code=code; this.message=message; } @Override publicIntegergetCode(){ returncode; } @Override publicStringgetMessage(){ returnmessage; } }
//返回正确结果带返回值。 ServiceResult,String>s=ServiceResult.success(Arrays.asList("1","2","3")); s.isSuccess();//true ServiceResult,Integer>e1=ServiceResult.error(CodeMessageEnum.some_error); e1.isSuccess();//false ServiceResult,Long>e2=ServiceResult.error(newServiceResult.DefaultMessage<>(1000L,"error")); e2.isSuccess();//false //ServiceResult{data=[1,2,3],message=DefaultMessage{code=00000000,message='success'},isSuccess=true} System.out.println(s); //ServiceResult{data=null,message=DefaultMessage{code=1001,message='错误信息。'},isSuccess=false} System.out.println(e1); //ServiceResult{data=null,message=DefaultMessage{code=1000,message='error'},isSuccess=false} System.out.println(e2);
补充知识:java封装返回结果实体类返回结果以及错误信息
我就废话不多说了,大家还是直接看代码吧~
publicclassResponseMessage{ privatefinalstaticStringSTATUS_OK="0"; privatefinalstaticStringSTATUS_ERROR="1"; privateStringstatus;//状态 privateObjectdata;//返回值 privateExceptione;//异常类捕获 privateStringmsg;//自定义信息 publicStringgetStatus(){ returnstatus; } publicvoidsetStatus(Stringstatus){ this.status=status; } publicObjectgetData(){ returndata; } publicvoidsetData(Objectdata){ this.data=data; } publicExceptiongetE(){ returne; } publicvoidsetE(Exceptione){ this.e=e; } publicStringgetMsg(){ returnmsg; } publicvoidsetMsg(Stringmsg){ this.msg=msg; } publicResponseMessage(){ super(); } publicResponseMessage(Stringstatus,Objectdata,Exceptione,Stringmsg){ super(); this.status=status; this.data=data; this.e=e; this.msg=msg; } /** *成功的结果 *@paramdata返回结果 *@parammsg返回信息 */ publicstaticResponseMessagenewOkInstance(Objectdata,Stringmsg){ returnnewResponseMessage(ResponseMessage.STATUS_OK,data,null,msg); } /** *成功的结果 *@paramdata返回结果 */ publicResponseMessagenewOkInstance(Objectdata){ returnnewResponseMessage(ResponseMessage.STATUS_OK,data,null,null); } /** *失败的结果 *@parammsg返回信息 */ publicstaticResponseMessagenewErrorInstance(Stringmsg){ returnnewResponseMessage(ResponseMessage.STATUS_ERROR,null,null,msg); } /** *失败的结果 *@parame异常对象 *@parammsg返回信息 */ publicstaticResponseMessagenewErrorInstance(Exceptione,Stringmsg){ returnnewResponseMessage(ResponseMessage.STATUS_ERROR,null,e,msg); } }
以上这篇Java封装统一的ResultModel案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。