前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot 统一结果集处理器

SpringBoot 统一结果集处理器

作者头像
木字楠
发布2022-11-15 16:51:40
5890
发布2022-11-15 16:51:40
举报
文章被收录于专栏:木字楠の空间

SpringBoot 统一结果集处理器(模板)

HttpResponseEnum

代码语言:javascript
复制
public enum HttpResponseEnum {
    /**
     * 操作成功
     */
    SUCCESS(20000, "操作成功!"),
    /**
     * 没有操作权限
     */
    AUTHORIZED(40003, "没有操作权限!"),
    /**
     * 系统异常
     */
    SYSTEM_ERROR(50000, "系统异常"),
    /**
     * 操作失败
     */
    FAIL(50000, "操作失败!"),
    /**
     * 参数格式不合法
     */
    VALID_ERROR(52000,"参数格式不合法!"),
    /**
     * 用户名已经存在
     */
    USERNAME_EXIST(52001, "用户名已经存在!");

    private final Integer code;
    private final String message;

    HttpResponseEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

第一种方式

ServerResponse

代码语言:javascript
复制
@Data
@Builder
@ApiModel("统一结果集处理类")
@AllArgsConstructor
@NoArgsConstructor
public class ServerResponse {

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

    @ApiModelProperty(value = "状态")
    private Boolean status;

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

    @ApiModelProperty(value = "数据")
    private Map<String, Object> data;

    /**
     * 访问成功
     *
     * @return ServerResponse
     */
    public static ServerResponse success() {
        return ServerResponse.builder()
                .status(true)
                .code(HttpResponseEnum.SUCCESS.getCode())
                .message(HttpResponseEnum.SUCCESS.getMessage())
                .data(new HashMap<>())
                .build();
    }

    /**
     * 访问失败
     *
     * @return ServerResponse
     */
    public static ServerResponse error() {
        return ServerResponse.builder()
                .status(false)
                .code(HttpResponseEnum.FAIL.getCode())
                .message(HttpResponseEnum.FAIL.getMessage())
                .data(new HashMap<>())
                .build();
    }

    /**
     * 设置消息信息
     *
     * @param message 消息
     * @return ServerResponse
     */
    public ServerResponse message(String message) {
        this.message = message;
        return this;
    }

    /**
     * 设置 状态码 和 信息 (一)
     *
     * @param code    状态码
     * @param message 信息
     * @return ServerResponse
     */
    public ServerResponse codeAndMessage(Integer code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }


    /**
     * 设置 状态码 和 信息 (一)
     *
     * @param httpResponseEnum 枚举信息
     * @return ServerResponse
     */
    public ServerResponse codeAndMessage(HttpResponseEnum httpResponseEnum) {
        this.code = httpResponseEnum.getCode();
        this.message = httpResponseEnum.getMessage();
        return this;
    }

    /**
     * 设置 数据 (一)
     *
     * @param key   key
     * @param value value  ==> Object
     * @return ServerResponse
     */
    public ServerResponse data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    /**
     * 设置 数据 (二)
     *
     * @param data Map 集合
     * @return ServerResponse
     */
    public ServerResponse data(Map<String, Object> data) {
        this.data = data;
        return this;
    }
}

第二种方式

ResponseResult

代码语言:javascript
复制
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("统一结果集处理器")
public class ResponseResult<T> {

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

    /**
     * 状态信息
     */
    @ApiModelProperty(value = "状态信息")
    private Boolean status;

    /**
     * 返回信息
     */
    @ApiModelProperty(value = "返回信息")
    private String message;

    /**
     * 数据
     */
    @ApiModelProperty(value = "数据")
    private T data;

    /**
     *  全参数方法
     * @param code 状态码
     * @param status 状态
     * @param message 返回信息
     * @param data 返回数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    private static <T> ResponseResult<T> response(Integer code, Boolean status, String message, T data) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(code);
        responseResult.setStatus(status);
        responseResult.setMessage(message);
        responseResult.setData(data);
        return responseResult;
    }

    /**
     *  全参数方法
     * @param code 状态码
     * @param status 状态
     * @param message 返回信息
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    private static <T> ResponseResult<T> response(Integer code, Boolean status, String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(code);
        responseResult.setStatus(status);
        responseResult.setMessage(message);
        return responseResult;
    }

    /**
     * 成功返回(无参)
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success() {
        return response(HttpResponseEnum.SUCCESS.getCode(), true, HttpResponseEnum.SUCCESS.getMessage(), null);
    }

    /**
     * 成功返回(枚举参数)
     * @param httpResponseEnum 枚举参数
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(HttpResponseEnum httpResponseEnum) {
        return response(httpResponseEnum.getCode(), true, httpResponseEnum.getMessage());
    }

    /**
     * 成功返回(状态码+返回信息)
     * @param code 状态码
     * @param message 返回信息
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(Integer code, String message) {
        return response(code, true, message);
    }

    /**
     * 成功返回(返回信息 + 数据)
     * @param message 返回信息
     * @param data 数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(String message, T data) {
        return response(HttpResponseEnum.SUCCESS.getCode(), true, message, data);
    }

    /**
     * 成功返回(状态码+返回信息+数据)
     * @param code 状态码
     * @param message 返回信息
     * @param data 数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(Integer code, String message, T data) {
        return response(code, true, message, data);
    }

    /**
     * 成功返回(数据)
     * @param data 数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> success(T data) {
        return response(HttpResponseEnum.SUCCESS.getCode(), true, HttpResponseEnum.SUCCESS.getMessage(), data);
    }

    /**
     * 失败返回(无参)
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail() {
        return response(HttpResponseEnum.FAIL.getCode(), false, HttpResponseEnum.FAIL.getMessage(), null);
    }

    /**
     *  失败返回(枚举)
     * @param httpResponseEnum 枚举
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(HttpResponseEnum httpResponseEnum) {
        return response(httpResponseEnum.getCode(), false, httpResponseEnum.getMessage());
    }

    /**
     *  失败返回(状态码+返回信息)
     * @param code 状态码
     * @param message 返回信息
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(Integer code, String message) {
        return response(code, false, message);
    }

    /**
     *  失败返回(返回信息+数据)
     * @param message 返回信息
     * @param data 数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(String message, T data) {
        return response(HttpResponseEnum.FAIL.getCode(), false, message, data);
    }

    /**
     *  失败返回(状态码+返回信息+数据)
     * @param code 状态码
     * @param message 返回消息
     * @param data 数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(Integer code, String message, T data) {
        return response(code, false, message, data);
    }

    /**
     *  失败返回(数据)
     * @param data 数据
     * @param <T> 泛型
     * @return {@link ResponseResult<T>}
     */
    public static <T> ResponseResult<T> fail(T data) {
        return response(HttpResponseEnum.FAIL.getCode(), false, HttpResponseEnum.FAIL.getMessage(), data);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot 统一结果集处理器(模板)
    • HttpResponseEnum
    • 第一种方式
      • ServerResponse
      • 第二种方式
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档