Java 数据返回通用类

第一种(简单定义)

import lombok.Data;

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

/**
 * 返回通用类
 * @param <T>
 */
@Data
public class R<T> {
    /** 编码:1成功,0和其它数字为失败*/
    private Integer code;
    /** 信息返回*/
    private String msg;
    /** 信息返回数据*/
    private T data;
    /** 动态数据*/
    private Map map = new HashMap();
    public static <T> R<T> success(T object) {
        R<T> r = new R<T>();
        r.data = object;
        r.code = 1;
        return r;
    }
    public static <T> R<T> error(String msg) {
        R r = new R();
        r.msg = msg;
        r.code = 0;
        return r;
    }
    public R<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}

第二种

导入依赖

<dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.13</version>
            <scope>compile</scope>
</dependency>

编写结果数据通用类

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * todo JsonInclude 注解忽略 null值 看业务需求添加
 *
 * @author Chunming Liu In 2022/07/28
 */
@ApiModel("分页响应参数")
@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
@Data
public class JsonResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "结果状态", example = "200")
    private Integer code = 200;
    @ApiModelProperty(value = "结果数据")
    private T data;
    @ApiModelProperty(value = "结果描述", example = "成功")
    private String message = "ok";

    public JsonResult() {
    }

    public JsonResult(Integer code) {
        this();
        this.code = code;
    }

    public JsonResult(Integer code, T data) {
        this(code);
        this.data = data;
    }

    public JsonResult(Integer code, String message) {
        this(code);
        this.message = message;
    }

    public JsonResult(Integer code, String message, T data) {
        this(code, message);
        this.data = data;
    }

    public static <T> JsonResult<T> success() {
        return new JsonResult(200);
    }

    public static <T> JsonResult<T> success(T data) {
        return new JsonResult(200, data);
    }

    public static <T> JsonResult<T> success(String message, T data) {
        return new JsonResult(200, message, data);
    }

    public static <T> JsonResult<T> success(Integer code, String message, T data) {
        return new JsonResult(code, message, data);
    }

    public static JsonResult<String> error() {
        return new JsonResult(500);
    }

    public static <T> JsonResult<T> error(String message) {
        return new JsonResult(500, message);
    }

    public static <T> JsonResult<T> error(Integer code, String message) {
        return new JsonResult(code, message);
    }

    public static <T> JsonResult<T> error(Integer code, String message, T data) {
        return new JsonResult(code, message, data);
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值