ruoyi框架, 使用 Swagger接口文档改造AjaxResult类方便使用, 使用 Swagger接口文档改造AjaxResult类方便使用

避坑(根据改造Result方法, 其extends HashMap,自己未注意导致😓,特意记录一下):

需要生成getter方法,或者使用@Data (common-core未引入依赖)!!!,不然会出现调用接口返回数据:

{success=true, error=false}

代码如下:

package com.ruoyi.common.core.web.domain;

import com.ruoyi.common.core.constant.HttpStatus;

import java.io.Serializable;
import java.util.Objects;

/**
 * swagger文档可用
 * @author peng
 * @date 2023/7/17
 */

public class AjaxResultNew<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private String msg;

    private int code;

    private T data;

    public AjaxResultNew() {
    }

    /**
     * 初始化一个新创建的 AjaxResultNew 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public AjaxResultNew(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 初始化一个新创建的 AjaxResultNew 对象
     *
     * @param code 状态码
     * @param msg  返回内容
     * @param data 数据对象
     */
    public AjaxResultNew(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static AjaxResultNew success() {
        return AjaxResultNew.success("操作成功");
    }

    /**
     * 返回成功数据T
     *
     * @return 成功消息
     */
    public static  <U> AjaxResultNew<U> success(U data) {
        return AjaxResultNew.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static AjaxResultNew success(String msg) {
        return AjaxResultNew.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static  <U> AjaxResultNew<U> success(String msg, U data) {
        return new AjaxResultNew(HttpStatus.SUCCESS, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static AjaxResultNew error() {
        return AjaxResultNew.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static AjaxResultNew error(String msg) {
        return AjaxResultNew.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static <U> AjaxResultNew<U> error(String msg, U data) {
        return new AjaxResultNew(HttpStatus.ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */
    public static AjaxResultNew error(int code, String msg) {
        return new AjaxResultNew(code, msg, null);
    }

    /**
     * 是否为成功消息
     *
     * @return 结果
     */
    public boolean isSuccess() {
        return Objects.equals(HttpStatus.SUCCESS, this.code);
    }

    /**
     * 是否为错误消息
     *
     * @return 结果
     */
    public boolean isError() {
        return !isSuccess();
    }


    public String getMsg() {
        return msg;
    }

    public int getCode() {
        return code;
    }

    public T getData() {
        return data;
    }
}
Logo

快速构建 Web 应用程序

更多推荐