zoukankan      html  css  js  c++  java
  • 后端接口统一返回响应对象

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

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

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

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

  • 相关阅读:
    const 函数
    为什么要进行初始化(C语言)
    关于矩阵的逆
    在写论文的参考文献时,有的段落空格很大,有的段落则正常,原因及解决方法(wps)
    C#递归搜索指定目录下的文件或目录
    try catch finally 执行顺序面试题总结
    关于try catch finally的执行顺序解释 都有return
    C# 序列号和反序列化 对象写入bin
    C# 序列化和反序列化 详解 对象和XML文件之间
    在C#中,Json的序列化和反序列化的几种方式总结
  • 原文地址:https://www.cnblogs.com/yanggb/p/10995404.html
Copyright © 2011-2022 走看看