1、 高複用服務響應對象java
1 package com.mmall.common; 2 3 import com.fasterxml.jackson.annotation.JsonIgnore; 4 import com.fasterxml.jackson.databind.annotation.JsonSerialize; 5 6 import java.io.Serializable; 7 8 /** 9 * 高複用服務響應對象 10 * 11 * Created by ly on 2018/6/11. 12 */ 13 @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) 14 public class ServerResponse<T> implements Serializable { 15 16 /** 17 * 狀態碼 18 */ 19 private int status; 20 21 /** 22 * 響應信息 23 */ 24 private String msg; 25 26 /** 27 * 響應數據 28 */ 29 private T data; 30 31 /** 32 * 構造函數 33 * 34 * @param status 35 */ 36 private ServerResponse(int status) { 37 this.status = status; 38 } 39 40 private ServerResponse(int status, String msg) { 41 this.status = status; 42 this.msg = msg; 43 } 44 45 private ServerResponse(int status, T data) { 46 this.status = status; 47 this.data = data; 48 } 49 50 private ServerResponse(int status, String msg, T data) { 51 this.status = status; 52 this.msg = msg; 53 this.data = data; 54 } 55 56 /** 57 * 根據狀態碼返回是否成功 58 * JsonIgnore使被註解內容不在json序列化結果當中 59 * 60 * @return 61 */ 62 @JsonIgnore 63 public boolean isSuccess() { 64 return this.status == ResponseCode.SUCCESS.getCode(); 65 } 66 67 /** 68 * 暴露獲取屬性的方法 69 * 70 * @return 71 */ 72 public int getStatus() { 73 return this.status; 74 } 75 76 public String getMsg() { 77 return this.msg; 78 } 79 80 public T getData() { 81 return this.data; 82 } 83 84 /** 85 * 經過該公開方法直接獲取一個成功狀態碼的本對象 86 * 87 * @param <T> 88 * @return 89 */ 90 public static <T> ServerResponse<T> createBySuccess() { 91 return new ServerResponse<T>(ResponseCode.SUCCESS.getCode()); 92 } 93 94 public static <T> ServerResponse<T> createBySuccess(String msg) { 95 return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg); 96 } 97 98 public static <T> ServerResponse<T> createBySuccess(T data) { 99 return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), data); 100 } 101 102 public static <T> ServerResponse<T> createBySuccess(String msg, T data) { 103 return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data); 104 } 105 106 /** 107 * 獲取錯誤對象 108 * 109 * @param <T> 110 * @return 111 */ 112 public static <T> ServerResponse<T> createByError() { 113 return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc()); 114 } 115 116 public static <T> ServerResponse<T> createByError(String msg) { 117 return new ServerResponse<T>(ResponseCode.ERROR.getCode(), msg); 118 } 119 120 public static <T> ServerResponse<T> createByError(int errorCode, String errorMessage) { 121 return new ServerResponse<T>(errorCode, errorMessage); 122 } 123 124 }
2、 響應枚舉類json
1 package com.mmall.common; 2 3 /** 4 * 響應的枚舉類 5 * 6 * Created by ly on 2018/6/11. 7 */ 8 public enum ResponseCode { 9 10 SUCCESS(0,"SUCCESS"), 11 ERROR(1,"ERROR"), 12 NEED_LOGIN(10,"NEED_LOGIN"), 13 ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT"); 14 15 private final int code; 16 private final String desc; 17 18 // 這裏使用default的修飾,只容許類內部及本包調用 19 ResponseCode(int code, String desc) { 20 this.code = code; 21 this.desc = desc; 22 } 23 24 /** 25 * 暴露獲取屬性的方法 26 * 27 * @return 28 */ 29 public int getCode() { 30 return code; 31 } 32 public String getDesc() { 33 return desc; 34 } 35 }