import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize; import java.io.Serializable;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化json的时候,如果是null的对象,key也会消失
public class ServiceResponse<T> implements Serializable {
private int status; //结果状态值
private String msg; //结果信息
private T data; //结果数据 private ServiceResponse(int status){
this.status = status;
} private ServiceResponse(int status, T data){
this.status = status;
this.data = data;
} private ServiceResponse(int status, String msg, T data){
this.status = status;
this.msg = msg;
this.data = data;
} private ServiceResponse(int status, String msg){
this.status = status;
this.msg = msg;
} @JsonIgnore //让其不在json序列化中
public boolean isSuccess(){
return this.status == ResponseCode.SUCCESS.getCode();
} public int getStatus() {
return status;
} public String getMsg() {
return msg;
} public T getData() {
return data;
} public static <T> ServiceResponse<T> createBySuccess(){
return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode());
} public static <T> ServiceResponse<T> createBySuccessMessage(String msg){
return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), msg);
} public static <T> ServiceResponse<T> createBySuccess(T data){
return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), data);
} public static <T> ServiceResponse<T> createBySuccess(String msg, T data){
return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
} public static <T> ServiceResponse<T> createByError(){
return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
} public static <T> ServiceResponse<T> createByErrorMessage(String errorMessage){
return new ServiceResponse<T>(ResponseCode.ERROR.getCode(), errorMessage);
} public static <T> ServiceResponse<T> createByErrorCodeMessage(int errorCode, String errorMessage){
return new ServiceResponse<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;
}
}

最新文章

  1. [转]看部电影,透透彻彻理解IoC(你没有理由再迷惑!)
  2. Linux学习心得之 Linux下命令行Android开发环境的搭建
  3. 让我轻轻的告诉你AliSQLselect语句中in多少个合适
  4. 小记sql server临时表与表变量的区别
  5. Android下海康实时视频解码
  6. 怎样快速学会ZBrush 中的移动笔刷的运用
  7. ArcGIS API for Silverlight加载google地图(后续篇)
  8. iOS 瀑布流的基本原理
  9. 李洪强iOS之Foundation框架—字符串
  10. C#多线程开发
  11. 灵光乍现,lua数据绑定
  12. C#学习基础总结
  13. JavaScript之JS实现动画效果
  14. Android JNI programming demo with Eclipse
  15. Apache ab测试工具使用方法(无参、get传参、post传参)
  16. 用MATLAB结合四种方法搜寻罗马尼亚度假问题
  17. bootstrap-fileinput文件上传控件的亲身实践
  18. 〖C语言学习笔记 〗(一) HelloWorld
  19. React组件继承的由来
  20. 在python3下使用OpenCV 显示图像

热门文章

  1. SpringBoot学习(五)RSocket和Security
  2. robot framework设置更高级别的关键字
  3. Mysql注入绕过安全狗
  4. STATUS_STACK_BUFFER_OVERRUN不一定是栈缓冲区溢出
  5. hibernate实现增删改查
  6. Cogs 1632. 搬运工(二分图最小点覆盖)
  7. 训练集,验证集,测试集(以及为什么要使用验证集?)(Training Set, Validation Set, Test Set)
  8. GitHub页面基本知识
  9. 转载:基于 Hive 的文件格式:RCFile 简介及其应用---推酷
  10. 图解LinkedHashMap原理