统一封装json返回结果
2024-10-21 07:53:00
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;
}
}
最新文章
- [转]看部电影,透透彻彻理解IoC(你没有理由再迷惑!)
- Linux学习心得之 Linux下命令行Android开发环境的搭建
- 让我轻轻的告诉你AliSQLselect语句中in多少个合适
- 小记sql server临时表与表变量的区别
- Android下海康实时视频解码
- 怎样快速学会ZBrush 中的移动笔刷的运用
- ArcGIS API for Silverlight加载google地图(后续篇)
- iOS 瀑布流的基本原理
- 李洪强iOS之Foundation框架—字符串
- C#多线程开发
- 灵光乍现,lua数据绑定
- C#学习基础总结
- JavaScript之JS实现动画效果
- Android JNI programming demo with Eclipse
- Apache ab测试工具使用方法(无参、get传参、post传参)
- 用MATLAB结合四种方法搜寻罗马尼亚度假问题
- bootstrap-fileinput文件上传控件的亲身实践
- 〖C语言学习笔记 〗(一) HelloWorld
- React组件继承的由来
- 在python3下使用OpenCV 显示图像
热门文章
- SpringBoot学习(五)RSocket和Security
- robot framework设置更高级别的关键字
- Mysql注入绕过安全狗
- STATUS_STACK_BUFFER_OVERRUN不一定是栈缓冲区溢出
- hibernate实现增删改查
- Cogs 1632. 搬运工(二分图最小点覆盖)
- 训练集,验证集,测试集(以及为什么要使用验证集?)(Training Set, Validation Set, Test Set)
- GitHub页面基本知识
- 转载:基于 Hive 的文件格式:RCFile 简介及其应用---推酷
- 图解LinkedHashMap原理