返回结果封装类

返回结果封装类

返回结果封装类

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rduck.common.entity.Result;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import springfox.documentation.spring.web.json.Json;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.UiConfiguration;

import java.util.ArrayList;

/**
 * 返回结果包装类
 *
 * @author yz
 */
@RestControllerAdvice
@RequiredArgsConstructor
public class ResultEncapsulationHandler implements ResponseBodyAdvice<Object> {
    private final ObjectMapper objectMapper;

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @SneakyThrows
    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if (o == null) {
            return null;
        }
        if (o instanceof ArrayList && ((ArrayList) o).size() == 0) {
            return Result.success(o);
        }
        // 这里需要过滤掉swagger的相关返回
        if (o instanceof Result || o instanceof Json || o instanceof UiConfiguration || (o instanceof ArrayList && ((ArrayList) o).get(0) instanceof SwaggerResource)) {
            return o;
        }

        //处理字符串类型数据
        if (o instanceof String) {
            return objectMapper.writeValueAsString(Result.success(o));
        }
        return Result.success(o);
    }
}
package com.rduck.common.entity;


import cn.hutool.json.JSONUtil;
import com.rduck.common.enums.ResultEnum;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;

/**
 * @author yz
 */
@Data
@ToString
@Builder
@SuppressWarnings("unchecked")
public class Result<T> {

    /**
     * 错误码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 具体的内容
     */
    private T data;

    private Result(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data = data;
    }

    private Result(String msg) {
        this.code = ResultEnum.ERROR.getCode();
        this.data = null;
        this.msg = msg;
    }

    private Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    private Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功时调用, 没有data内容
     *
     * @return
     */
    public static <String> Result<String> success() {
        return Result.build(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), null);
    }

    /**
     * description
     * 成功时候的调用
     *
     * @param data 数据
     * @return {@link Result }<{@link T }>
     * @author yz
     * @date 2022/07/10
     **/
    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    /**
     * 根据返回的状态枚举, 构建返回结果
     *
     * @param resultEnum {@link ResultEnum} 返回状态枚举
     * @return Result
     */
    public static <Object> Result<Object> build(ResultEnum resultEnum) {
        return new Result<>(resultEnum);
    }

    /**
     * 根据自定义状态码{@code code}和自定义提示信息{@code msg}构建返回结果
     *
     * @param code 自定义状态码
     * @param msg  自定义提示信息
     * @return Result
     */
    public static <Object> Result<Object> build(Integer code, String msg) {
        return new Result<>(code, msg);
    }

    /**
     * 根据自定义状态码{@code code}, 自定义提示信息{@code msg}以及返回实体{@code T}构建返回结果
     *
     * @param code 自定义状态码
     * @param msg  自定义提示信息
     * @param <T>  返回实体的类型
     * @return Result
     */
    public static <T> Result<T> build(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    /**
     * 出错时调用, 自定义提示信息{@code msg}
     *
     * @param msg 自定义提示信息
     * @param <T> 返回实体的类型
     * @return Result
     */
    public static <T> Result<T> error(String msg) {
        return new Result<>(msg);
    }

    /**
     * 出错时调用, 根据返回实体{@code T}构建返回结果
     *
     * @param data 返回实体
     * @param <T>  返回实体的类型
     * @return Result
     */
    public static <T> Result<T> error(T data) {
        return new Result<>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), data);
    }

    /**
     * description 转json字符串
     *
     * @return {@link String }
     * @author yz
     * @date 2022/07/10
     **/
    public String toJson() {
        return JSONUtil.toJsonStr(new Result(this.code, this.msg, this.data));
    }
}