package commonsResult;
import java.io.Serializable;
public class ResultResponse<T> implements Serializable {
private int code;
private String message;
private T data;
private ResultResponse(int code){
this.code=code;
}
private ResultResponse(int code,String message){
this(code);
this.message=message;
}
private ResultResponse(int code,String message,T data){
this(code,message);
this.data = data;
}
public static ResultResponse success(){
return new ResultResponse(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMessage());
}
public static<T> ResultResponse success(T t){
ResultResponse success = success();
success.setData(t);
return success;
}
public static<T> ResultResponse fail(){
return new ResultResponse(ResultEnum.FAIL.getCode(),ResultEnum.FAIL.getMessage());
}
public static<T> ResultResponse fail(ResultEnum resultEnum){
return new ResultResponse(resultEnum.getCode(),resultEnum.getMessage());
}
public static<T> ResultResponse fail(ResultEnum resultEnum,T data){
ResultResponse fail = fail(resultEnum);
fail.setData(data);
return fail;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public boolean isSuccess(){
return this.code==ResultEnum.SUCCESS.getCode();
}
}
package commonsResult;
public enum ResultEnum {
SUCCESS(601,"请求成功"),
FAIL(701,"请求失败"),
USERNAME_ERROR(705,"用户名不存在"),
PASSWORD_ERROR(706,"密码错误"),
NO_LOGIN(702,"未登录"),
NO_AUTHORITY(703,"权限不足"),
PARAM_ERROR(704,"请求参数错误"),
USERNAME_REPEAT(707,"用户名重复"),
TELEPHONE_REPEAT(708,"该手机已经注册"),
EMAIL_REPEAT(709,"该邮箱已经注册"),;
private int code;
private String message;
ResultEnum(int code,String message){
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}