不灭的焱

革命尚未成功,同志仍须努力 下载Java21

作者:AlbertWen  添加时间:2026-03-05 09:59:13  修改时间:2026-03-26 15:16:35  分类:07.Java框架/系统  编辑

Result类功能:封装需要向前端传递的数据。

Result类的内部构造:在内部封装三个元素,code,msg,T data;和两个构造方法,一个用来传递成功信息,另一个用来传递失败信息。

package com.fuyo.dic.framework.model;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一响应结果
 * 
 * @author AlbertWen
 * @since 2026-03-01
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> implements Serializable {
    public static final Integer CODE_OK = 0; // 成功状态只有1个
    private static final Integer CODE_ERROR = 1; // 失败状态有多个
    private static final String MSG_OK = "操作成功";
    private static final String MSG_ERROR = "操作失败";

    private Integer code;
    private String msg;
    private T data;

    /**
     * 设置 状态码
     * 
     * @param <T>  数据类型
     * @param code 状态码
     * @return 自己对象
     */
    public R<T> code(Integer code) {
        this.code = code;
        return this;
    }

    /**
     * 设置 消息
     * 
     * @param <T> 数据类型
     * @param msg 消息
     * @return 自己对象
     */
    public R<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 设置 数据
     * 
     * @param <T>  数据类型
     * @param data 数据
     * @return 自己对象
     */
    public R<T> data(T data) {
        this.data = data;
        return this;
    }

    /**
     * 设置 成功状态
     * 
     * @param <T> 数据类型
     * @return 自己对象
     */
    public R<T> success() {
        this.code = CODE_OK;
        if (this.msg.isEmpty() || this.msg.equals(MSG_ERROR)) {
            this.msg = MSG_OK;
        }
        return this;
    }

    /**
     * 设置 失败状态
     * 
     * @param <T> 数据类型
     * @return 自己对象
     */
    public R<T> fail() {
        this.code = CODE_ERROR;
        if (this.msg.isEmpty() || this.msg.equals(MSG_OK)) {
            this.msg = MSG_ERROR;
        }
        return this;
    }

    /**
     * 成功响应结果
     * 
     * @param <T> 数据类型
     * @return
     */
    public static <T> R<T> ok() {
        return new R<>(CODE_OK, MSG_OK, null);
    }

    /**
     * 成功响应结果,根据值类型自动设置 状态码、消息、数据
     * 
     * @param <T>   数据类型
     * @param value 设置值
     * @return
     */
    public static <T> R<T> ok(T value) {
        // 值为:消息
        if (value instanceof String) {
            return new R<>(CODE_OK, (String) value, null);
        }
        // 值为:状态码
        else if (value instanceof Integer) {
            return new R<>((Integer) value, MSG_ERROR, null);
        }
        // 值为:数据
        return new R<>(CODE_OK, MSG_OK, value);
    }

    /**
     * 成功响应结果,设置 消息、数据
     * 
     * @param <T>  数据类型
     * @param msg  消息
     * @param data 数据
     * @return
     */
    public static <T> R<T> ok(String msg, T data) {
        return new R<>(CODE_OK, msg, data);
    }

    /**
     * 成功响应结果,设置 状态码、消息、数据
     * 
     * @param <T>  数据类型
     * @param code 状态码
     * @param msg  消息
     * @param data 数据
     * @return
     */
    public static <T> R<T> ok(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    /**
     * 失败响应结果
     * 
     * @param <T> 数据类型
     * @return
     */
    public static <T> R<T> error() {
        return new R<>(CODE_ERROR, MSG_ERROR, null);
    }

    /**
     * 失败响应结果,根据值类型自动设置 状态码、消息、数据
     * 
     * @param <T>   数据类型
     * @param value 设置值
     * @return
     */
    public static <T> R<T> error(T value) {
        // 值为:消息
        if (value instanceof String) {
            return new R<>(CODE_ERROR, (String) value, null);
        }
        // 值为:状态码
        else if (value instanceof Integer) {
            return new R<>((Integer) value, MSG_ERROR, null);
        }
        // 值为:数据
        return new R<>(CODE_ERROR, MSG_ERROR, value);
    }

    /**
     * 失败响应结果,设置 状态码、消息
     * 
     * @param <T>  数据类型
     * @param code 状态码
     * @param msg  消息
     * @return
     */
    public static <T> R<T> error(Integer code, String msg) {
        return new R<>(code, msg, null);
    }

    /**
     * 失败响应结果,设置 状态码、消息、数据
     * 
     * @param <T>  数据类型
     * @param code 状态码
     * @param msg  消息
     * @param data 数据
     * @return
     */
    public static <T> R<T> error(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

}