參考鏈接: 用Java封裝
定義自己所需要的返回值類型?
public class CodeMsg implements Cloneable {
? ? private int retCode;
? ? private String message;
?
? ? // 通用異常
? ? public static CodeMsg SUCCESS = new CodeMsg(0, "success");
? ? public static CodeMsg EMPTY_PARAM_ERROR = new CodeMsg(400, "參數為空");
? ? public static CodeMsg INTER_ERROR = new CodeMsg(505, "服務端異常");
?
? ? private CodeMsg(int retCode, String message) {
? ? ? ? this.retCode = retCode;
? ? ? ? this.message = message;
? ? }
?
? ? public int getRetCode() {
? ? ? ? return retCode;
? ? }
?
? ? public String getMessage() {
? ? ? ? return message;
? ? }
?
? ? public void setMessage(String message) {
? ? ? ? this.message = message;
? ? }
?
? ? @Override
? ? protected Object clone() throws CloneNotSupportedException {
? ? ? ? return (CodeMsg) super.clone();
? ? }
}?
返回值的封裝?
public class Result<T> {
? ? private String message;
? ? private int retCode;
? ? private T data;
?
? ? private Result(T data) {
? ? ? ? this.retCode = 200;
? ? ? ? this.message = "成功";
? ? ? ? this.data = data;
? ? }
?
? ? private Result(CodeMsg cm) {
? ? ? ? if (cm == null) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? this.retCode = cm.getRetCode();
? ? ? ? this.message = cm.getMessage();
? ? }
?
? ? /**
? ? ?* 成功時候的調用
? ? ?*
? ? ?* @return
? ? ?*/
? ? public static <T> Result<T> success(T data) {
? ? ? ? return new Result<T>(data);
? ? }
?
? ? /**
? ? ?* 成功,不需要傳入參數
? ? ?*
? ? ?* @return
? ? ?*/
? ? @SuppressWarnings("unchecked")
? ? public static <T> Result<T> success() {
? ? ? ? return (Result<T>) success("");
? ? }
?
? ? /**
? ? ?* 失敗時候的調用
? ? ?*
? ? ?* @return
? ? ?*/
? ? public static <T> Result<T> error(CodeMsg cm) {
? ? ? ? return new Result<T>(cm);
? ? }
?
? ? /**
? ? ?* 失敗時候的調用,擴展消息參數
? ? ?*
? ? ?* @param cm
? ? ?* @param msg
? ? ?* @return
? ? ?*/
? ? public static <T> Result<T> error(CodeMsg cm, String msg) {
? ? ? ? CodeMsg newCodeMsg = null;
? ? ? ? try {
? ? ? ? ? ? newCodeMsg = (CodeMsg) cm.clone();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? newCodeMsg.setMessage(cm.getMessage() + "--" + msg);
? ? ? ? return new Result<T>(newCodeMsg);
? ? }
?
? ? public T getData() {
? ? ? ? return data;
? ? }
?
? ? public String getMessage() {
? ? ? ? return message;
? ? }
?
? ? public int getRetCode() {
? ? ? ? return retCode;
? ? }
?
?
}?
參考返回碼定義?
// 成功狀態碼
public static final int SUCCESS = 1;
?
// -------------------失敗狀態碼----------------------
// 參數錯誤
public static final int PARAMS_IS_NULL = 10001;// 參數為空
public static final int PARAMS_NOT_COMPLETE = 10002; // 參數不全
public static final int PARAMS_TYPE_ERROR = 1003; // 參數類型錯誤
public static final int PARAMS_IS_INVALID = 10004; // 參數無效
?
// 用戶錯誤
public static final int USER_NOT_EXIST = 20001; // 用戶不存在
public static final int USER_NOT_LOGGED_IN = 20002; // 用戶未登陸
public static final int USER_ACCOUNT_ERROR = 20003; // 用戶名或密碼錯誤
public static final int USER_ACCOUNT_FORBIDDEN = 20004; // 用戶賬戶已被禁用
public static final int USER_HAS_EXIST = 20005;// 用戶已存在
?
// 業務錯誤
public static final int BUSINESS_ERROR = 30001;// 系統業務出現問題
?
// 系統錯誤
public static final int SYSTEM_INNER_ERROR = 40001; // 系統內部錯誤
?
// 數據錯誤
public static final int DATA_NOT_FOUND = 50001; // 數據未找到
public static final int DATA_IS_WRONG = 50002;// 數據有誤
public static final int DATA_ALREADY_EXISTED = 50003;// 數據已存在
?
// 接口錯誤
public static final int INTERFACE_INNER_INVOKE_ERROR = 60001; // 系統內部接口調用異常
public static final int INTERFACE_OUTER_INVOKE_ERROR = 60002;// 系統外部接口調用異常
public static final int INTERFACE_FORBIDDEN = 60003;// 接口禁止訪問
public static final int INTERFACE_ADDRESS_INVALID = 60004;// 接口地址無效
public static final int INTERFACE_REQUEST_TIMEOUT = 60005;// 接口請求超時
public static final int INTERFACE_EXCEED_LOAD = 60006;// 接口負載過高
?
// 權限錯誤
public static final int PERMISSION_NO_ACCESS = 70001;// 沒有訪問權限