Java项目实现API返回结果统一封装(通用)

为什么需要将返回结果统一封装

  1. 在我们实际的业务开发中,返回结果统一化可以提高我们后端人员自身的编码效率

  1. 团队开发所有返回数据集能保持一致,可以快速处理数据

  1. 约定返回集减少了前后端人员每次因数据不一致而产生隔阂

返回具体实现类

为了方便编码,我使用了lombok

/**
 * @author
 * @email [email protected]
 * @date 2023/2/26 14:17
 * @desciption: 全局统一返回结果类
 */
@Data
@Builder
@AllArgsConstructor
public class ResultDto implements Serializable{

    private static final long serialVersionUID = 1L;
    /**
     * 返回码
     */
    private ResultCode code;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 数据数
     */
    private long total;
    /**
     * 返回数据
     */
    private T data;

    public ResultDto(){this.total = Long.valueOf(0L);}

    public ResultDto(ResultCode code){this.code = code;}

    public ResultDto(ResultCode code, T data){
        super();
        this.code = code;
        this.data = data;
        this.message = code.getMessage();
    }

    public String getCode(){
        return this.code.getCode();
    }

    public String getCodeMessage(){
        return this.code.getMessage();
    }
    public static   ResultDto ok(){
        return new ResultDto(ResultCode.OK);
    }

    public static  ResultDto ok(T data){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .data(data)
                .message(ResultCode.OK.getMessage())
                .build();
    }

    public static  ResultDto ok(T data, long total) {
        return ResultDto.builder()
                .code(ResultCode.OK)
                .data(data)
                .total(total)
                .message(ResultCode.OK.getMessage())
                .build();
    }

    public static  ResultDto ok(String message){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .message(message)
                .build();
    }

    public static  ResultDto ok(String message, T data){
        return ResultDto.builder()
                .code(ResultCode.OK)
                .message(message)
                .data(data)
                .build();
    }

    public static  ResultDto error(){
        return ResultDto.builder()
                .code(ResultCode.ERROR)
                .message(ResultCode.ERROR.getMessage())
                .data(null)
                .build();
    }

    public static  ResultDto error(String message){
        return ResultDto.builder()
                .code(ResultCode.ERROR)
                .message(message == null || message.isEmpty() ? ResultCode.ERROR.getMessage() : message)
                .data(null)
                .build();
    }

    public boolean isSuccess(){
        if (this.getCode() == ResultCode.OK.name()){
            return true;
        }
        return false;
    }
}

返回状态码枚举类

此处只提供最常用的状态,实际开发中针对不同业务场景肯定远远不止如此,具体可以根据业务实际需求增改

/**
 * @author Yunnuo
 * @email [email protected]
 * @date 2023/2/26 15:25
 * @desciption: 全局统一返回结果状态码
 */
@Getter
public enum ResultCode {

    OK("200", "成功"),
    ERROR("201", "失败"),
    PARAM_ERROR("202", "参数不正确"),
    SERVICE_ERROR("203", "服务异常"),
    DATA_ERROR("204","数据异常"),
    NOT_LOG_IN("205","未登录"),
    NO_ACCESS("206", "没有权限"),
    TOKEN_ERROR("207", "token错误");

    private String code;
    private String message;

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

    public String getCode(){return this.code;}

    public String getMessage(){return this.message;}

    public static ResultCode codeOf(String code){
        ResultCode status = resolve(code);
        if (status == null){
            throw new IllegalArgumentException("No matching constant for [" + code + "]");
        }
        return status;
    }

    public static ResultCode resolve(String statusCode){
        for (ResultCode resultCode : values()){
            if (resultCode.code == statusCode){
                return resultCode;
            }
        }
        return null;
    }

    public String toString(){
        return this.code + " " + name();
    }
}

你可能感兴趣的:(Java进阶,java,spring,jvm)