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

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

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

  • 相关阅读:
    应用图标大小
    AndroidStudio使用笔记
    shell 三剑客之 sed 命令详解
    shell 三剑客之 sed pattern 详解
    shell 文本处理三剑客之 grep 和 egrep
    Shell 编程中的常用工具
    shell 函数的高级用法
    shell 数学运算
    shell 变量的高级用法
    nginx 之 https 证书配置
  • 原文地址:https://www.cnblogs.com/yanggb/p/10995404.html
Copyright © 2011-2022 走看看