R.java 2.43 KB
Newer Older
licc's avatar
licc committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
package cn.wisenergy.common.utils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.http.HttpStatus;

import java.io.Serializable;

/**
 * 响应信息类
 *
 * @author lut
 */
@Data
@ApiModel(description = "响应信息主体")
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty("返回标记:成功标记=0,失败标记=-1")
    private int code;
    @ApiModelProperty("返回信息")
    private String message;
    @ApiModelProperty("数据")
    private T data;

    public R() {
        this.code = 0;
        this.message = "success";
    }

    public R(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public R(int code, T data) {
        this.code = code;
        this.data = data;
    }

    public R(T data) {
        this.code = 0;
        this.message = "success";
        this.data = data;
    }

    public R(T data, String msg) {
        this.code = 0;
        this.message = "success";
        this.data = data;
        this.message = msg;
    }

    /**
     * 请求成功
     */
    public static <T> R<T> ok() {
        return new R<>();
    }

    /**
     * 请求成功,返回前端的信息
     *
     * @param msg 描述
     * @return R
     */
    public static <T> R<T> ok(String msg, T data) {
        return new R<>(0, data);
    }

    /**
     * 请求成功,返回前端的信息
     *
     * @param data 返回值
     * @return R
     */
    public static <T> R<T> ok(T data) {
        return new R<>(0, data);
    }

    /**
     * 请求成功,返回前端信息
     *
     * @param code 状态码
     * @param data 返回值
     * @return R
     */
    public static <T> R<T> ok(int code, T data) {
        return new R<>(code, data);
    }

    /**
     * 请求失败
     *
     * @return R
     */
    public static <T> R<T> error() {
        return new R<>(-1, "操作失败");
    }

    /**
     * 请求失败,返回前台信息
     *
     * @param msg 描述
     * @return R
     */
    public static <T> R<T> error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    /**
     * 请求失败,返回前台信息
     *
     * @param code 错误码
     * @param msg  描述
     * @return R
     */
    public static <T> R<T> error(int code, String msg) {
        return new R<>(code, msg);
    }

}