Last active
July 24, 2018 03:11
-
-
Save zwdgit/66402254f992e07e5aee41ed7300bbc1 to your computer and use it in GitHub Desktop.
通用响应对象
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) | |
/* | |
JsonSerialize 注解的作用就是:保证序列化 json 的时候,如果是 null 的对象,key 也会消失。 | |
部分响应对象不需要将字段全部返回前端,例如‘失败’的时候只需要返回 status 和 msg ,Data 是不需要返回的, | |
此时返回前端时,只有一个 key ,而 value 为空,所以需要进行配置,将 value 为空的字段进行忽略 | |
*/ | |
public class ServerResponse<T> implements Serializable { | |
private int status; | |
private String msg; | |
private T Data; | |
private ServerResponse(int status) { | |
this.status = status; | |
} | |
private ServerResponse(int status, String msg) { | |
this.status = status; | |
this.msg = msg; | |
} | |
private ServerResponse(int status, T data) { | |
this.status = status; | |
Data = data; | |
} | |
private ServerResponse(int status, String msg, T data) { | |
this.status = status; | |
this.msg = msg; | |
Data = data; | |
} | |
/* | |
* 框架对属性的调用都遵循java been规范,也就是getXXX,setXXX,isXXX等等, | |
* 而不是直接访问属性值,所以框架会通过isXXX来获取XXX属性的值 | |
*/ | |
@JsonIgnore | |
//使之不在 json 序列化结果当中 | |
public boolean isSuccess() { | |
return this.status == ResponseCode.SUCCESS.getCode(); | |
} | |
public int getStatus() { | |
return status; | |
} | |
public T getData() { | |
return Data; | |
} | |
public String getMsg() { | |
return msg; | |
} | |
public static <T> ServerResponse<T> createBySuccess() { | |
return new ServerResponse<>(ResponseCode.SUCCESS.getCode()); | |
} | |
public static <T> ServerResponse<T> createBySuccessMessage(String msg) { | |
return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), msg); | |
} | |
public static <T> ServerResponse<T> createBySuccess(T data) { | |
return new ServerResponse<>(ResponseCode.SUCCESS.getCode(), data); | |
} | |
public static <T> ServerResponse<T> createBySuccess(String msg, T data) { | |
return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data); | |
} | |
public static <T> ServerResponse<T> createByError() { | |
return new ServerResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc()); | |
} | |
public static <T> ServerResponse<T> createByErrorMessage(String errorMessage) { | |
return new ServerResponse<T>(ResponseCode.ERROR.getCode(), errorMessage); | |
} | |
public static <T> ServerResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage) { | |
return new ServerResponse<T>(errorCode, errorMessage); | |
} | |
} | |
/** | |
* 响应状态码的枚举类 | |
*/ | |
public enum ResponseCode { | |
SUCCESS(0,"SUCCESS"), | |
ERROR(1,"ERROR"), | |
NEED_LOGIN(10,"NEED_LOGIN"), | |
ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT"); | |
private final int code; | |
private final String desc; | |
ResponseCode(int code, String desc) { | |
this.code = code; | |
this.desc = desc; | |
} | |
public int getCode() { | |
return code; | |
} | |
public String getDesc() { | |
return desc; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment