• 响应体和状态码


    后端响应体和状态码设计

    主流技术:响应体 和 状态码结合使用

    响应体:数据 响应 给前端的 格式

    1、为什么要设计统一响应体?

    1、系统默认提供许多的状态码,但HTTP的状态码数量有限。
    通过修改响应返回的JSON数据,更好的表达业务中遇到的情况。

    2、目前后端主流RESTful API的数据接口,提高效率。

    2、了解最基础的统一响应体

    建议采用泛型,而不是采用Object。系统结合Swagger2使用时,Object可能有问题,采用泛型设计就能够读取到list中的字段信息。

    /**
     * 统一 响应体(返回类)
     * @param  具体数据对象类型
     */
    @Data//自动生成getter、setter、equals、hashCode和toString方法
    public class BaseResponse<T> implements Serializable {
    
        private int code;
    
        private T data;
    
        private String message;
    
        //构造函数:在创建对象时为对象的成员变量赋初始值。不需要用户来调用它,在建立对象时自动执行。
        public BaseResponse(int code, T data, String message) {
            this.code = code;
            this.data = data;
            this.message = message;
        }
    
        public BaseResponse(int code, T data) {
            this(code, data, "");
        }
    
        /**
         * 错误 响应体
         * 统一 响应体 调用 错误状态码ErrorCode。
         * ErrorCode 包括 (code + 错误 返回的响应体)
         * @param errorCode
         */
        public BaseResponse(ErrorCode errorCode) {
            this(errorCode.getCode(), null, errorCode.getMessage());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    3、状态码设计

    最好设计:枚举类

    错误 状态码

    /**
     * 自定义错误码
     */
    public enum ErrorCode {
        /**
         * 组成:错误 状态码 + 错误响应体
         *                  code + message
         */
        SUCCESS(0, "ok"),
        PARAMS_ERROR(40000, "请求参数错误"),
        NOT_LOGIN_ERROR(40100, "未登录"),
        NO_AUTH_ERROR(40101, "无权限"),
        NOT_FOUND_ERROR(40400, "请求数据不存在"),
        FORBIDDEN_ERROR(40300, "禁止访问"),
        SYSTEM_ERROR(50000, "系统内部异常"),
        OPERATION_ERROR(50001, "操作失败");
    
        /**
         * 错误响应体 状态码
         */
        private final int code;
    
        /**
         * 错误响应体 信息
         */
        private final String message;
    
        ErrorCode(int code, String message) {
            this.code = code;
            this.message = message;
        }
    
        public int getCode() {
            return code;
        }
    
        public String getMessage() {
            return message;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    4、使用

    /**
     * 返回工具类
     */
    public class ResultUtils {
    
        /**
         * 成功
         *
         * @param data
         * @param 
         * @return
         */
        public static <T> BaseResponse<T> success(T data) {
            return new BaseResponse<>(0, data, "ok");
        }
    
        /**
         * 失败
         *
         * @param errorCode
         * @return
         */
        public static BaseResponse error(ErrorCode errorCode) {
            return new BaseResponse<>(errorCode);
        }
    
        /**
         * 失败
         *
         * @param code
         * @param message
         * @return
         */
        public static BaseResponse error(int code, String message) {
            return new BaseResponse(code, null, message);
        }
    
        /**
         * 失败
         *
         * @param errorCode
         * @return
         */
        public static BaseResponse error(ErrorCode errorCode, String message) {
            return new BaseResponse(errorCode.getCode(), null, message);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    localhost知识
    web渗透测试----5、暴力破解漏洞--(2)SNMP密码破解
    uniapp使用抖音微信自定义组件
    电子签名如何制作——word
    扩展Conda的宇宙:使用conda config --append channels命令
    python使用代码
    最详解的正则表达式------元字符
    java.io.IOException: Unable to establish loopback connection
    Guava-EventBus 源码解析
    spring boot自动装配及自动装配条件判断
  • 原文地址:https://blog.csdn.net/weixin_55008454/article/details/134446352