• 后端接口统一返回响应对象


    在项目开发中,一般返回给前端的都会是一个统一的返回响应对象,因此后端需要封装一个泛型类来作为响应对象,这样做的好处是前后端能统一接口返回,可以做规范的响应处理。

    ServerResponse类

    // 保证序列化json的时候,如果是null的对象,key也会消失
    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
    public class ServerResponse<T> implements Serializable {
        private int status;
        private String msg;
        private T data;
    
        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;
        }
    
        @JsonIgnore
        // 使之不在json序列化结果当中
        public boolean isSuccess() {
            return this.status == ResponseCode.SUCCESS.getCode();
        }
    
        public int getStatus() {
            return status;
        }
    
        public T getData() {
            return data;
        }
    
        public String getMsg() {
            return msg;
        }
    
        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);
        }
    }

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

    没有什么说的,直接贴代码就完事了,实际项目可以根据实际情况进行微调。

    "你偷偷地把心情藏起来了不让我知道,只是你不会知道我已经知道了。"

  • 相关阅读:
    oracle表分区管理partition
    winfrom 界面编辑之疑难杂症
    resx文件引用
    mysql 数据库名含“-”
    KNN算法之图像处理二
    KNN算法之图像处理一
    DCOM初步窥探二
    dcom初步窥探一
    .net组件和com组件&托管代码和非托管代码
    c# apache服务器请求得到数据(初级)
  • 原文地址:https://www.cnblogs.com/yanggb/p/10995404.html
Copyright © 2020-2023  润新知