1. Packaging result, the returned object as
public class the Result <T> {
Private int code;
Private String MSG;
Private Data T;
/ **
* successful call time
* * /
public static <T> the Result <T> Success ( Data T) {
return new new the Result <T> (Data);
}
/ **
* when the call failed
* * /
public static <T> the Result <T> error (codeMsg codeMsg) {
return new new the Result <T> (codeMsg) ;
}
/ **
* successful constructor
* @param Data
* /
Private the Result (T Data) {
this.code = 000000; 000000 // default is successful
this.msg = "sUCCESS";
this.data = Data;
}
the Result Private (int code, String MSG) {
this.code = code;
this.msg MSG =;
}
/ **
* constructor fail
* @param code
* @param MSG
* /
Private the Result (CodeMsg codeMsg) {
IF ( ! codeMsg = null) {
this.code codeMsg.getCode = ();
this.msg codeMsg.getMsg = ();
}
}
GET (), SET ();
}
2. package CodeMsg, there is stored the code corresponding to the definition of MSG
public class CodeMsg {
Private int code;
Private String MSG;
// generic error code
public static CodeMsg SUCCESS = new CodeMsg ( 0, "success");
public static CodeMsg SERVER_ERROR = new CodeMsg ( 500100, " Exception server");
public static CodeMsg BIND_ERROR new new CodeMsg = (500 101, "verification error parameters:% S");
// login module 5002XX
public static CodeMsg SESSION_ERROR = new new CodeMsg (500210, "Session does not exist or has failed");
public static CodeMsg PASSWORD_EMPTY = new new CodeMsg (500 211, "password can not be empty");
public static CodeMsg MOBILE_EMPTY = new new CodeMsg (500 212, "the phone number can not be empty") ;
public static CodeMsg MOBILE_ERROR = new new CodeMsg (500 213, "phone number format error");
public static CodeMsg MOBILE_NOT_EXIST = new new CodeMsg (500 214, "the phone number does not exist");
public static CodeMsg PASSWORD_ERROR = new new CodeMsg (500 215, "the password is wrong ");
// commodity module 5003XX
// Orders module 5004XX
// spike module 5005XX
Private CodeMsg () {
}
private CodeMsg( int code,String msg ) {
this.code = code;
this.msg = msg;
}
//不定参的构造函数
public fillArgs(Object... args) {
int code = this.code;
String message = String.format(this.msg, args);
return new CodeMsg(code, message);
}
@Override
public String toString() {
return "CodeMsg [code=" + code + ", msg=" + msg + "]";
}
get(),set()方法
}
3.应用实例
return Result.success(user);
return Result.error(CodeMsg.MOBILE_ERROR);
The most authoritative json custom format
Guess you like
Origin www.cnblogs.com/uzxin/p/11860996.html
Recommended
Ranking