Java封装一个通用返回类,用于返回统一json数据

渡星河
2023-04-13 / 0 评论 / 36 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年06月19日,已超过457天没有更新,若内容或图片失效,请留言反馈。

网上找了很多都感觉和自己的业务不匹配就封装了

package com.galaxy.empvue.common;

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.io.Serial;
import java.io.Serializable;

@Data
@Accessors(chain = true)
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private int code; // 响应状态码
    private String message; // 响应信息
    private T data; // 响应数据

    // 无参构造函数,默认响应状态码为 200,响应信息为 "success"
    public R() {
        this.code = HttpStatus.OK.value();
        this.message = "success";
    }

    // 带有响应数据的构造函数,响应状态码为 200,响应信息为 "success"
    public R(T data) {
        this();
        if (data != null) {
            this.data = data;
        }
    }

    // 自定义响应状态码和响应信息的构造函数
    public R(int code, String message) {
        this.code = code;
        this.message = message;
    }

    // 带有响应数据、响应状态码和响应信息的构造函数
    public R(T data, int code, String message) {
        this(code, message);
        if (data != null) {
            this.data = data;
        }
    }

    // 返回一个默认的成功响应对象,响应状态码为 200,响应信息为 "success"
    public static <T> R<T> success() {
        return new R<>();
    }

    // 返回一个带有响应数据的成功响应对象,响应状态码为 200,响应信息为 "success"
    public static <T> R<T> success(T data) {
        return new R<>(data);
    }

    // 返回一个带有自定义响应信息的成功响应对象,响应状态码为 200
    public static <T> R<T> success(String message) {
        return new R<T>().setMessage(message);
    }

    // 返回一个带有响应数据和自定义响应信息的成功响应对象,响应状态码为 200
    public static <T> R<T> success(T data, String message) {
        return new R<>(data).setMessage(message);
    }

    // 返回一个带有自定义响应状态码和响应信息的失败响应对象
    public static <T> R<T> failure(int code, String message) {
        return new R<>(code, message);
    }

    // 返回一个带有自定义响应信息的失败响应对象,响应状态码为 500
    public static <T> R<T> failure(String message) {
        return new R<T>()
                .setCode(HttpStatus.INTERNAL_SERVER_ERROR.value())
                .setMessage(message)
                .setData(null); // 将 data 字段设置为 null
    }


    // 返回一个默认的失败响应对象,响应状态码为 500,响应信息为 "操作失败"
    public static <T> R<T> failure() {
        return new R<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "操作失败");
    }
}

第二种

package cn.yyx.common;

import cn.yyx.utils.ResultCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

//统一返回结果的类
@Data
public class R {

    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    //把构造方法私有
    private R() {
    }

    //成功静态方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(200);
        r.setMessage("成功");
        return r;
    }

    public static R error() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(-1);
        r.setMessage("操作失败");
        return r;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    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 Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}

若依通用返回类

package com.ruoyi.common.core.domain;

import java.util.HashMap;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.StringUtils;

/**
 * 操作消息提醒
 * 
 * @author ruoyi
 */
public class AjaxResult extends HashMap<String, Object>
{
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";

    /**
     * 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
     */
    public AjaxResult()
    {
    }

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

    /**
     * 初始化一个新创建的 AjaxResult 对象
     * 
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public AjaxResult(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (StringUtils.isNotNull(data))
        {
            super.put(DATA_TAG, data);
        }
    }

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

    /**
     * 返回成功数据
     * 
     * @return 成功消息
     */
    public static AjaxResult success(Object data)
    {
        return AjaxResult.success("操作成功", data);
    }

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

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

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

    /**
     * 返回警告消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static AjaxResult warn(String msg, Object data)
    {
        return new AjaxResult(HttpStatus.WARN, msg, data);
    }

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

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

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

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

    /**
     * 方便链式调用
     *
     * @param key 键
     * @param value 值
     * @return 数据对象
     */
    @Override
    public AjaxResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }
}
1

评论 (0)

取消