Java封裝統(tǒng)一的Result Model案例
在開發(fā)過程中,有時(shí)候會(huì)需要使用錯(cuò)誤碼+錯(cuò)誤信息的形式,來返回某些業(yè)務(wù)操作的錯(cuò)誤結(jié)果信息,來代替效率較低的異常傳遞。
這樣就需要封裝一個(gè)統(tǒng)一的Result model作為返回值,代替直接返回?cái)?shù)據(jù)等結(jié)果。
1.定義錯(cuò)誤碼 - 錯(cuò)誤信息接口
錯(cuò)誤碼可能是String、Integer、Long 等類型,也可能是enum類型。因此這里使用泛型來代替,錯(cuò)誤碼類型,可以提高靈活性
。也可以通過枚舉直接實(shí)現(xiàn)CodeMessage接口。
public interface CodeMessage<C>{ C getCode(); String getMessage();}
2.實(shí)現(xiàn)Result Model
這里定義的是service層的Result。有時(shí)候在controller層只會(huì)作一些簡(jiǎn)單的參數(shù)校驗(yàn),在service層會(huì)作進(jìn)一步的校驗(yàn),
如果controller中需要統(tǒng)一返回一個(gè)JsonResult可以將ServiceResult作一個(gè)簡(jiǎn)單的轉(zhuǎn)換器進(jìn)行轉(zhuǎn)換。
public class ServiceResult<T, C> implements Serializable { public static final CodeMessage<String> SUCCESS = new DefaultMessage<>('00000000', 'success'); private T data; private CodeMessage<C> message; private boolean isSuccess; ServiceResult(T data, boolean isSuccess, CodeMessage<C> message) { this.data = data; this.message = message; this.isSuccess = isSuccess; } public T getData() { return data; } public CodeMessage<C> getCodeMessage() { return message; } public boolean isSuccess() { return isSuccess; } public static <D, C> ServiceResultBuilder<D, C> success() { ServiceResultBuilder<D, C> builder = builder(); return builder.isSuccess(true); } public static <D, C> ServiceResultBuilder<D, C> success(CodeMessage<C> codeMessage) { ServiceResultBuilder<D, C> builder = builder(); return builder.isSuccess(true).code(codeMessage.getCode()).message(codeMessage.getMessage()); } public static <D> ServiceResult<D, String> success(D data) { ServiceResultBuilder<D, String> success = success(SUCCESS); return success.data(data).build(); } public static <D, C> ServiceResult<D, C> error(CodeMessage<C> codeMessage) { ServiceResultBuilder<D, C> builder = builder(); return builder.isSuccess(false).code(codeMessage.getCode()).message(codeMessage.getMessage()).build(); } public static <D, C> ServiceResultBuilder<D, C> error() { ServiceResultBuilder<D, C> builder = builder(); return builder.isSuccess(false); } static <D, C> ServiceResultBuilder<D, C> builder() { return new ServiceResultBuilder<>(); } public static class DefaultMessage<C> implements CodeMessage<C>, java.io.Serializable { private C code; private String message; public DefaultMessage(C code, String message) { this.code = code; this.message = message; } @Override public C getCode() { return code; } @Override public String getMessage() { return message; } @Override public String toString() { return 'DefaultMessage{' + 'code=' + code + ', message=’' + message + ’’’ + ’}’; } } public static class ServiceResultBuilder<T, C> { private T data; private C code; private String message; private boolean isSuccess; ServiceResultBuilder() { //package private } public ServiceResultBuilder<T, C> data(T data) { this.data = data; return this; } ServiceResultBuilder<T, C> isSuccess(boolean isSuccess) { this.isSuccess = isSuccess; return this; } public ServiceResultBuilder<T, C> code(C code) { this.code = code; return this; } public ServiceResultBuilder<T, C> message(String message) { this.message = message; return this; } public ServiceResult<T, C> build() { Objects.requireNonNull(code, 'code'); Objects.requireNonNull(code, 'message'); return new ServiceResult<>(data, isSuccess, new DefaultMessage<>(code, message)); } } @Override public String toString() { return 'ServiceResult{' +'data=' + data +', message=' + message +', isSuccess=' + isSuccess +’}’; }}
3.使用
定義枚舉錯(cuò)誤碼
pubilc enum CodeMessageEnum implements CodeMessage<Integer> { some_error(1001, '錯(cuò)誤信息。'); Integer code; String message; CodeMessageEnum(Integer code, String message) { this.code = code; this.message = message; } @Override public Integer getCode() { return code; } @Override public String getMessage() { return message; }}
//返回正確結(jié)果帶返回值。ServiceResult<List<String>, String> s = ServiceResult.success(Arrays.asList('1', '2', '3'));s.isSuccess(); //trueServiceResult<?, Integer> e1 = ServiceResult.error(CodeMessageEnum.some_error);e1.isSuccess(); //falseServiceResult<?, Long> e2 = ServiceResult.error(new ServiceResult.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=’錯(cuò)誤信息。’}, isSuccess=false}System.out.println(e1);//ServiceResult{data=null, message=DefaultMessage{code=1000, message=’error’}, isSuccess=false}System.out.println(e2);
補(bǔ)充知識(shí):java 封裝返回結(jié)果實(shí)體類 返回結(jié)果以及錯(cuò)誤信息
我就廢話不多說了,大家還是直接看代碼吧~
public class ResponseMessage { private final static String STATUS_OK = '0'; private final static String STATUS_ERROR = '1'; private String status; // 狀態(tài) private Object data; // 返回值 private Exception e; // 異常類捕獲 private String msg; // 自定義信息 public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public Exception getE() { return e; } public void setE(Exception e) { this.e = e; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public ResponseMessage() { super(); } public ResponseMessage(String status, Object data, Exception e, String msg) { super(); this.status = status; this.data = data; this.e = e; this.msg = msg; } /** * 成功的結(jié)果 * @param data 返回結(jié)果 * @param msg 返回信息 */ public static ResponseMessage newOkInstance(Object data, String msg) { return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, msg); } /** * 成功的結(jié)果 * @param data 返回結(jié)果 */ public ResponseMessage newOkInstance(Object data) { return new ResponseMessage(ResponseMessage.STATUS_OK, data, null, null); } /** * 失敗的結(jié)果 * @param msg 返回信息 */ public static ResponseMessage newErrorInstance(String msg) { return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, null, msg); } /** * 失敗的結(jié)果 * @param e 異常對(duì)象 * @param msg 返回信息 */ public static ResponseMessage newErrorInstance(Exception e, String msg) { return new ResponseMessage(ResponseMessage.STATUS_ERROR, null, e, msg); }}
以上這篇Java封裝統(tǒng)一的Result Model案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳細(xì)總結(jié)Java for循環(huán)的那些坑2. 新手學(xué)python應(yīng)該下哪個(gè)版本3. Java進(jìn)行Appium自動(dòng)化測(cè)試的實(shí)現(xiàn)4. 如何利用Python matplotlib繪制雷達(dá)圖5. python對(duì)批量WAV音頻進(jìn)行等長(zhǎng)分割的方法實(shí)現(xiàn)6. jsp文件下載功能實(shí)現(xiàn)代碼7. uni-app結(jié)合PHP實(shí)現(xiàn)單用戶登陸demo及解析8. 聊聊python在linux下與windows下導(dǎo)入模塊的區(qū)別說明9. ajax實(shí)現(xiàn)頁(yè)面的局部加載10. php中PHPUnit框架實(shí)例用法

網(wǎng)公網(wǎng)安備