刘光辉
15 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.util;
 
import lombok.Data;
 
import java.io.Serializable;
 
/**
 * 通用返回结果类
 */
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
 
    /**
     * 状态码
     */
    private int code;
 
    /**
     * 消息
     */
    private String msg;
 
    /**
     * 数据
     */
    private T data;
 
    /**
     * 成功
     */
    private boolean success;
 
    /**
     * 构造方法
     */
    private R() {
    }
 
    /**
     * 构造方法
     */
    private R(int code, String msg, T data, boolean success) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
    }
 
    /**
     * 成功返回
     */
    public static <T> R<T> success() {
        return new R<>(200, "操作成功", null, true);
    }
 
    /**
     * 成功返回带数据
     */
    public static <T> R<T> success(T data) {
        return new R<>(200, "操作成功", data, true);
    }
 
    /**
     * 成功返回带消息和数据
     */
    public static <T> R<T> success(String msg, T data) {
        return new R<>(200, msg, data, true);
    }
 
    /**
     * 失败返回
     */
    public static <T> R<T> error() {
        return new R<>(500, "操作失败", null, false);
    }
 
    /**
     * 失败返回带消息
     */
    public static <T> R<T> error(String msg) {
        return new R<>(500, msg, null, false);
    }
 
    /**
     * 失败返回带状态码和消息
     */
    public static <T> R<T> error(int code, String msg) {
        return new R<>(code, msg, null, false);
    }
 
    /**
     * 失败返回带状态码、消息和数据
     */
    public static <T> R<T> error(int code, String msg, T data) {
        return new R<>(code, msg, data, false);
    }
}