Java后端数据返回通用类

280 阅读1分钟

 第一种(简单定义)

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值 看业务需求添加
 */
@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);
    }


}