响应体和状态码

后端响应体和状态码设计

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

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

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

1、系统默认提供许多的状态码,但HTTP的状态码数量有限。

通过修改响应返回的JSON数据,更好的表达业务中遇到的情况。
2、目前后端主流RESTful API的数据接口,提高效率。

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

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

java 复制代码
/**
 * 统一 响应体(返回类)
 * @param <T> 具体数据对象类型
 */
@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());
    }
}

3、状态码设计

最好设计:枚举类
错误 状态码

java 复制代码
/**
 * 自定义错误码
 */
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;
    }
}

4、使用

java 复制代码
/**
 * 返回工具类
 */
public class ResultUtils {

    /**
     * 成功
     *
     * @param data
     * @param <T>
     * @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);
    }
}
相关推荐
用户8307196840821 小时前
Spring Boot WebClient性能比RestTemplate高?看完秒懂!
java·spring boot
Assby3 小时前
从洋葱模型看Java与Go的设计哲学:为什么它们如此不同?
java·后端·架构
belhomme4 小时前
(面试题)Netty 线程模型
java·面试·netty
NE_STOP9 小时前
MyBatis-plus进阶之映射与条件构造器
java
Seven9711 小时前
NIO的零拷贝如何实现高效数据传输?
java
架构师沉默1 天前
别又牛逼了!AI 写 Java 代码真的行吗?
java·后端·架构
后端AI实验室1 天前
我把一个生产Bug的排查过程,交给AI处理——20分钟后我关掉了它
java·ai
凉年技术1 天前
Java 实现企业微信扫码登录
java·企业微信
狂奔小菜鸡1 天前
Day41 | Java中的锁分类
java·后端·java ee