Fork me on GitHub

还在使用ResponseEntity?试试自己构建一个优雅的响应返回类和通用分页结果类

直接上代码

1. 通用响应实体类

import java.util.Objects;

/**
 * <p> 响应实体类 </p>
 *
 * @author lishaohui
 * @since 2023/9/20 19:58
 */
public class Response<T> {

    /**
     * 返回结果状态码
     */
    private Integer code;

    /**
     * 返回结果状态信息
     */
    private String message;

    /**
     * 返回主体数据
     */
    private T data;

    protected Response() {

    }

    protected Response(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 构造通用成功返回{@link Result Response}工厂方法
     *
     * @return {@link Result Response}
     */
    public static <T> Response<T> ok() {
        return Response.ok(null);
    }

    /**
     * 构造通用成功返回{@link Response Response}工厂方法
     *
     * @return {@link Response Response}
     */
    public static <T> Response<T> ok(T data) {
        return Response.of(BaseResponseStatus.SUCCESS_CODE, BaseResponseStatus.SUCCESS_MESSAGE, data);
    }

    /**
     * 构造通用失败返回{@link Result Response}工厂方法
     *
     * @return {@link Result Response}
     */
    public static <T> Response<T> error() {
        return Response.error(null);
    }

    /**
     * 构造通用失败返回{@link Result Result}工厂方法
     *
     * @param message 返回消息
     * @return {@link Result Result}
     */
    public static <T> Response<T> error(String message) {
        return Response.of(BaseResponseStatus.ERROR_CODE, message);
    }

    /**
     * 构造通用失败返回{@link Result Result}工厂方法
     *
     * @param code    返回状态码
     * @param message 返回消息
     * @return {@link Result Result}
     */
    public static <T> Response<T> error(Integer code, String message) {
        return Response.of(code, message);
    }

    /**
     * 构造{@link Response Response}工厂方法
     *
     * @param code    自定义返回码
     * @param message 自定义返回消息
     * @return {@link Response Response}
     */
    public static <T> Response<T> of(Integer code, String message) {
        return Response.<T>builder()
                .code(code)
                .message(message)
                .data(null)
                .build();
    }

    /**
     * 构造{@link Response Response}工厂方法
     *
     * @param code    自定义返回码
     * @param message 自定义返回消息
     * @param data    返回数据
     * @return {@link Response Response}
     */
    public static <T> Response<T> of(int code, String message, T data) {
        return Response.<T>builder()
                .code(code)
                .message(message)
                .data(data)
                .build();
    }

    /**
     * 构造{@link Response Response}工厂方法
     *
     * @param status 自定义返回格式
     * @param data   返回数据
     * @return {@link Response Response}
     */
    public static <T> Response<T> of(BaseResponseStatus status, T data) {
        return Response.<T>builder()
                .code(status.getCode())
                .message(status.getMessage())
                .data(data)
                .build();
    }

    public Response(Builder<T> tBuilder) {
        this.code = tBuilder.code;
        this.message = tBuilder.message;
        this.data = tBuilder.data;
    }

    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    public static <T> Builder<T> builder(Response<T> response) {
        Builder<T> builder = new Builder<>();
        builder.code = response.code;
        builder.message = response.message;
        builder.data = response.data;
        return builder;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Response<?> response = (Response<?>) o;
        return Objects.equals(getCode(), response.getCode())
                && Objects.equals(getMessage(), response.getMessage())
                && Objects.equals(getData(), response.getData());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getCode(), getMessage(), getData());
    }

    @Override
    public String toString() {
        return "Response{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public static class Builder<T> {
        private Integer code;
        private String message;
        private T data;

        private Builder() {
        }

        public Builder<T> code(Integer code) {
            this.code = code;
            return this;
        }

        public Builder<T> message(String message) {
            this.message = message;
            return this;
        }

        public Builder<T> data(T data) {
            this.data = data;
            return this;
        }

        public Response<T> build() {
            return new Response<>(this);
        }

    }

    /**
     * Base响应状态接口
     */
    public interface BaseResponseStatus {

        int SUCCESS_CODE = 200;
        int ERROR_CODE = 500;

        String SUCCESS_MESSAGE = "成功";
        String ERROR_MESSAGE = "失败";

        /**
         * 获取状态码
         */
        Integer getCode();

        /**
         * 获取返回信息
         */
        String getMessage();

    }

    public enum ResponseStatus implements BaseResponseStatus {

        /**
         * 200 成功 使用约定的成功状态码
         */
        SUCCESS(BaseResponseStatus.SUCCESS_CODE, BaseResponseStatus.SUCCESS_MESSAGE),

        /**
         * 500 异常 使用约定的失败状态码
         */
        ERROR(BaseResponseStatus.ERROR_CODE, BaseResponseStatus.ERROR_MESSAGE),

        /**
         * 参数错误
         */
        ILLEGAL_ARGUMENT(400, "参数错误"),

        UNAUTHORIZED(401, "未认证"),

        FORBIDDEN(403, "未授权"),

        WRONG_PASSWORD(411, "账号或密码错误"),

        ACCOUNT_DISABLED(412, "账号已被禁用"),
        ;

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

        @Override
        public Integer getCode() {
            return code;
        }

        @Override
        public String getMessage() {
            return message;
        }

        /**
         * 返回状态码,{@link ResponseStatus#SUCCESS_CODE SUCCESS(200)}表示成功
         */
        private final Integer code;

        /**
         * 返回描述信息
         */
        private final String message;
    }
}

2. 响应结果测试

    public Response<String> stringResponse() {
        return Response.<String>builder()
                .code(201)
                .message("test message")
                .data("test data")
                .build();
    }

3.通用分页

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>通用分页返回</p>
 *
 * @author lishaohui
 * @since 2023/9/20 21:06
 */
public class Page<T> implements Serializable {

    /**
     * 总条数
     */
    private long count;

    /**
     * 当前页数据
     */
    private List<T> rows;

    private Page() {

    }

    private Page(long count, List<T> rows) {
        this.count = count;
        this.rows = rows;
    }

    public Page(Builder<T> builder) {
        this.count = builder.count;
        this.rows = builder.rows;
    }

    public static <T> Page<T> fill(long count, List<T> rows) {
        return Page.<T>builder()
                .count(count)
                .rows(rows)
                .build();
    }

    public static <T> Page<T> empty(){
        return Page.fill(0, new ArrayList<>());
    }

    public static <T> Builder<T> builder(Page<T> page) {
        Builder<T> builder = new Builder<>();
        builder.count = page.count;
        builder.rows = page.rows;
        return builder;
    }

    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page<?> page = (Page<?>) o;
        return getCount() == page.getCount() && Objects.equals(getRows(), page.getRows());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getCount(), getRows());
    }

    @Override
    public String toString() {
        return "Page{" +
                "count=" + count +
                ", rows=" + rows +
                '}';
    }

    public static class Builder<T> {
        private long count;
        private List<T> rows;

        private Builder() {
        }

        public Builder<T> count(long count) {
            this.count = count;
            return this;
        }

        public Builder<T> rows(List<T> rows) {
            this.rows = rows;
            return this;
        }

        public Page<T> build() {
            return new Page<>(this);
        }
    }
}

posted @ 2023-09-20 21:24  Hui_Li  阅读(126)  评论(0编辑  收藏  举报