zoukankan      html  css  js  c++  java
  • 统一封装json返回结果

    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;
        }
    }
  • 相关阅读:
    登陆中发现css页面不见了,变成了光秃秃的输入框问题
    servlet基础一
    解决问题:Error:(5, 59) java: 程序包javax.servlet.http不存在
    静态方法的引用
    登录功能之添加验证码(包含切换验证码)
    生成图片验证码(VerifyCode)
    idea编辑器中批量完成注释及批量取消注释
    jdbc连接测试
    利用IO流复制图片
    js 判空
  • 原文地址:https://www.cnblogs.com/FondWang/p/12050879.html
Copyright © 2011-2022 走看看