项目中用到,比较好用!!
用户前端与后台的交互数据,这个封装类中包含分页、数据、错误码、状态码等信息!!
亲测好用哦!
一个类DataWrapper
public class DataWrapper<T>{
private CallStatusEnum callStatus;
private ErrorCodeEnum errorCode;
private T data;
private int numberPerPage; // 用于分页结果
private int currentPage;
private int totalNumber;
private int totalPage;
private String message ;
private String recordUpdateTime ; //redis数据插入时间
public DataWrapper() {
callStatus = CallStatusEnum.SUCCEED;
errorCode = ErrorCodeEnum.No_Error;
}
public static <T> DataWrapper<T> createInstance(){
return new DataWrapper<T>() ;
}
public CallStatusEnum getCallStatus() {
return callStatus;
}
public DataWrapper<T> setCallStatus(CallStatusEnum callStatus) {
this.callStatus = callStatus;
return this ;
}
public ErrorCodeEnum getErrorCode() {
return errorCode;
}
public void setErrorCode(ErrorCodeEnum errorCode) {
this.errorCode = errorCode;
if (!errorCode.equals(ErrorCodeEnum.No_Error)) {
this.callStatus = CallStatusEnum.FAILED;
}
}
public T getData() {
return data;
}
public DataWrapper<T> setData(T data) {
this.data = data;
return this ;
}
public int getNumberPerPage() {
return numberPerPage;
}
public void setNumberPerPage(int numberPerPage) {
this.numberPerPage = numberPerPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public String getMessage() {
return message;
}
public DataWrapper<T> setMessage(String message) {
this.message = message;
return this ;
}
public String getRecordUpdateTime() {
return recordUpdateTime;
}
public DataWrapper<T> setRecordUpdateTime(String recordUpdateTime) {
this.recordUpdateTime = recordUpdateTime;
return this ;
}
@Override
public String toString() {
return JSONObject.toJSONString(this) ;
}
}
两个枚举CallStatusEnum、ErrorCodeEnum
public enum CallStatusEnum implements Serializable {
SUCCEED,
FAILED,;
CallStatusEnum() {
}
}
public enum ErrorCodeEnum implements Serializable {
No_Error("No ERROE!", 0),
Error("Unknown error!", 1),
NO_SUCH_USER("Can't find username",2),
IncorrectCredentials("IncorrectCredentialsException",3),
LoginFailed("AuthenticationException",4),
USER_EXIST("User already exist in db",5),
PASSWORD_ERROR("Error is password",6),
REGISTER_FAILED("Register failed",7),
PARSE_DATE("Parse Date error",8),
ExcessiveAttempts("Try password more than 5 times",9) ,
NO_ROLE("User has no role!" , 10) ,
USER_STATE_ERROR("User state error! Disable Login" , 11),
USER_IDENTITY_ERROR("User Identity error! Disable Login" , 12),
DB_ERROR("database maybe error!" , 13) ,
WEB_SP_ERROR("from webSp maybe error!" , 14) ,
MFA_VERIFY_ERROR("mfa verify random code error!" , 15) ,
MFA_SEND_ERROR("mfa send random code error!" , 16) ,
MFA_SEND_EXCESSIVE("ask send random code excessive!" , 17),
PHONE_NUMBER_ERROR("phone number is empty or error!" , 18),
IMAGECODE_ERROR("image code error" , 19),
VERIFYCODE_ERROR("telephone verify code incorrect",20);
private String label;
private Integer code;
ErrorCodeEnum() {
}
ErrorCodeEnum(String label, Integer code) {
this.label = label;
this.code = code;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public int getCode() {
return code;
}
@Override
public String toString() {
return code.toString();
}
public static ErrorCodeEnum parse(int code) {
for (ErrorCodeEnum theEnum : ErrorCodeEnum.values()) {
if (theEnum.getCode() == code) {
return theEnum;
}
}
return No_Error;
}
}