java通用返回对象

代码中定义了 三个成员变量

  1. status : 返回的状态码
  2. msg :返回的信息
  3. data :返回的对象.设置为泛型,以便扩展,服务端需要返回各种不一样的data数据,由此接收.

该类中私有了构造方法,保证了该类的准确性.定义了开发环境中需要返回的几种类型:

  1. status;

  2. status+msg;

  3. status+data;

  4. status+msg+data;


该类中也对外开放了各种静态方法:

  • createBySuccess() :返回带有成功status码的json数据

  • createBySuccessMassage(string msg) :返回带有成功status码和msg信息的json数据

  • createBySuccess(T data): :返回带有成功status码和data信息的json数据

  • createBySuccess(string msg,T data) :返回带有成功status码,msg信息,data信息的json数据

  • createByError():返回带有错误status码,错误msg信息的json数据

  • createByError(string msg):返回带有错误status码,自定义错误msg信息的json数据

  • createByError(int status,string msg):返回带有自定义错误status码,自定义错误msg信息的json数据


        //保证序列化json的时候,如果是null的对象,key也会消失
        @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
        public class ServerResponse 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;
                this.data = data;
            }
        
            private ServerResponse(int status,String msg,T data){
                this.status = status;
                this.msg = msg;
                this.data = data;
            }
        
            public int getStatus() {
                return status;
            }
        
            public String getMsg() {
                return msg;
            }
        
            public T getData() {
                return data;
            }
        
            /*判断当前状态是否成功.成功返回true.失败返回false
             */
            @JsonIgnore
            //使isSuccess返回值不被序列化,因为该方法不需要被转化成json对象
            public Boolean isSuccess(){
                return this.status == ResponseCode.SUCCESS.getCode();
            }
        
            //返回成功的ServerResponse对象.
            public static ServerResponse createBySuccess(){
                return new ServerResponse<>(ResponseCode.SUCCESS.getCode());
            }
            public static ServerResponse createBySuccessMassage(String msg){
                return new ServerResponse<>(ResponseCode.SUCCESS.getCode(),msg);
            }
            public static ServerResponse createBySuccess(T data){
                return new ServerResponse<>(ResponseCode.SUCCESS.getCode(),data);
            }
            public static ServerResponse createBySuccess(String msg,T data){
                return new ServerResponse<>(ResponseCode.SUCCESS.getCode(),msg,data);
            }
        
            //返回失败的ServerResponse对象.
            public static ServerResponse createByError(){
                return new ServerResponse<>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
            }
            public static ServerResponse createByError(String msg){
                return new ServerResponse<>(ResponseCode.ERROR.getCode(),msg);
            }
            public static ServerResponse createByError(int status,String msg){
                return new ServerResponse<>(status,msg);
            }
        
        
        }

以下是枚举类型的固定字符串,用于上述代码中使用.

 package com.mmall.common;
    
    public enum ResponseCode {
        SUCCESS(0,"SUCCESS"),
        ERROR(1,"ERROR");
        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;
        }
    
    }

你可能感兴趣的:(快捷开发,java)