【VIDEO_Parent】-项目搭建与创作者接口-统一返回数据格式

2023-11-29 22:06:02 浏览数 (1)

前言

在 common 模块中创建 common_utils 子模块

top.it6666.common_utils.utils 下创建一个接口为 状态码 新建 ResultCode 内容如下

代码语言:java复制
/**
 * @author BNTang
 */
public interface ResultCode {
    /**
     * 成功状态码
     */
    Integer SUCCESS = 20000;
    /**
     * 失败的状态码
     */
    Integer ERROR = 20001;
}

紧接着在 top.it6666.common_utils.utils 下创建一个统一返回结果的类 ResponseResult.java

代码语言:java复制
/**
 * @author BNTang
 */
@Data
public class ResponseResult {
    private ResponseResult() {
    }

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回的数据")
    private Map<String, Object> data = new HashMap<>();

    /**
     * 提供工具方法
     */
    public static ResponseResult ok() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setSuccess(true);
        responseResult.setCode(ResultCode.SUCCESS);
        responseResult.setMessage("成功");
        return responseResult;
    }

    public static ResponseResult error() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.setSuccess(false);
        responseResult.setCode(ResultCode.ERROR);
        responseResult.setMessage("失败");
        return responseResult;
    }

    public ResponseResult success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public ResponseResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public ResponseResult code(Integer code) {
        this.setCode(code);
        return this;
    }

    public ResponseResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public ResponseResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

}

最后

我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!

0 人点赞