美文网首页
SSM框架学习日记(2)——返回响应对象

SSM框架学习日记(2)——返回响应对象

作者: 糯米团子_大芒果 | 来源:发表于2018-07-01 00:20 被阅读0次

    接口

    调用接口时要返回状态码,返回的信息,返回的data,新建一个ServerResponse

        private int status;
        private String msg;
        private T data;
        //省略get方法
    

    把构造器变成私有的再提供共有的调用方法

    private ServerResponse(int status){
            this.status = status;
        }
    
        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;
        }
    
        private ServerResponse(int status,String msg){
            this.status = status;
            this.msg = msg;
        }
    

    声明一个枚举类型放响应码便于以后扩展,新建一个ResponseCode

    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;
        }
    }
    

    写一个判断响应是否成功的方法isSuccess(),等于success说明响应成功,JsonIgnore注解让这个方法不会被序列化显示在json中

    @JsonIgnore
        //不在序列号结果中
        public boolean isSuccess(){
            return this.status == ResponseCode.SUCCESS.getCode();
        }
    

    开放静态的共有方法

        public static <T> ServerResponse<T> createBySuccess(){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
        }
    
        public static <T> ServerResponse<T> createBySuccessMessage(String msg){
            return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
        }
    
        public static <T> ServerResponse<T> createBySuccess(T data){
            return new ServerResponse<T>(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);
        }
    

    在类名上加上注解JsonSerialize,返回时不需要的属性就不会显示出来

    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)//序列化json的时候不会有值为空的key
    public class ServerResponse<T> implements Serializable{
      ···
    }
    

    相关文章

      网友评论

          本文标题:SSM框架学习日记(2)——返回响应对象

          本文链接:https://www.haomeiwen.com/subject/rfwxuftx.html