MrShi
4 天以前 4fabfe4dbd2eb28d07a4350597d314958cc1c281
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.yzh.commons.model;
 
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.media.Schema;
import org.yzh.commons.util.StrUtils;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class APIResult<T> {
 
    public static final APIResult SUCCESS = new ImmutableAPIResult<>(new APIResult<>());
    public static final APIResult Unauthorized = new ImmutableAPIResult<>(new APIResult<>(APICodes.Unauthorized));
    public static final APIResult NotPermission = new ImmutableAPIResult<>(new APIResult<>(APICodes.NotPermission));
 
    public interface View {
    }
 
    @JsonView(View.class)
    @Schema(description = "响应码(成功:200;客户端错误:400-499;服务端错误:500-599)")
    protected int code;
    @JsonView(View.class)
    @Schema(description = "响应消息")
    protected String msg;
    @JsonView(View.class)
    @Schema(description = "响应消息详情")
    protected String detailMsg;
    @JsonView(View.class)
    @Schema(description = "响应数据")
    protected T data;
 
    public APIResult() {
        this.code = APICodes.Success.getCode();
        this.msg = APICodes.Success.getMessage();
    }
 
    public APIResult(Exception e) {
        this.code = APICodes.UnknownError.getCode();
        this.msg = e.getMessage();
        this.detailMsg = StrUtils.getStackTrace(e);
    }
 
    public APIResult(APICode code, Exception e) {
        this.code = code.getCode();
        this.msg = code.getMessage();
        this.detailMsg = e.getMessage();
    }
 
    public APIResult(APIException e) {
        this.code = e.getCode();
        this.msg = e.getMessage();
        this.detailMsg = e.getDetailMessage();
    }
 
    public APIResult(APICode code) {
        this.code = code.getCode();
        this.msg = code.getMessage();
    }
 
    public APIResult(APICode code, String message) {
        this.code = code.getCode();
        this.msg = message;
    }
 
    public APIResult(APICode code, String message, String detailMsg) {
        this.code = code.getCode();
        this.msg = message;
        this.detailMsg = detailMsg;
    }
 
    public APIResult(T t) {
        this(APICodes.Success, t);
    }
 
    public APIResult(APICode code, T data) {
        this(code);
        this.data = data;
    }
 
    public APIResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
 
    public static <T> APIResult<T> ok(T data) {
        return new APIResult<>(data);
    }
 
    public boolean isSuccess() {
        return APICodes.Success.getCode() == code;
    }
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public String getDetailMsg() {
        return detailMsg;
    }
 
    public void setDetailMsg(String detailMsg) {
        this.detailMsg = detailMsg;
    }
 
    public T getData() {
        return data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
 
    public static final class ImmutableAPIResult<T> extends APIResult<T> {
 
        public ImmutableAPIResult(APIResult<T> that) {
            this.code = that.code;
            this.msg = that.msg;
            this.detailMsg = that.detailMsg;
            this.data = that.data;
        }
 
        @Override
        public void setCode(int code) {
            throw new UnsupportedOperationException();
        }
 
        @Override
        public void setMsg(String msg) {
            throw new UnsupportedOperationException();
        }
 
        @Override
        public void setDetailMsg(String detailMsg) {
            throw new UnsupportedOperationException();
        }
 
        @Override
        public void setData(T data) {
            throw new UnsupportedOperationException();
        }
    }
}