Spring Boot - 通用响应类
# 响应类
一个规范的响应不应该只返回各种数据,而是要有一个规范的结果响应类,下面给出我使用的结果响应类(放在 http 包里):
/**
* @author kele-Bingtang
* @date 2022/4/30 14:47
* @note 响应对象
*/
public class Response<T> implements Serializable {
private static final long serialVersionUID = -464624820023286858L;
/** 自定义状态码 **/
private Integer code;
/** 状态码信息 **/
protected String status;
/** 消息 **/
private String message;
/** 时间戳 **/
private Long timestamp;
/** 数据 **/
protected transient T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> Response<T> instance() {
return new Response<>();
}
}
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
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
然后使用工具类进行处理并返回
/**
* @author kele-Bingtang
* @date 2022/4/30 14:55
* @note 请求响应封装
*/
public class HttpResult {
private HttpResult() {
}
public static <T> Response<T> response(T data, ResponseStatusEnum responseStatusEnum) {
Response<T> response = Response.instance();
response.setData(data);
response.setCode(responseStatusEnum.getCode());
response.setStatus(responseStatusEnum.getStatus());
response.setMessage(responseStatusEnum.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> response(T data, ResponseStatusEnum responseStatusEnum, String message) {
Response<T> response = Response.instance();
response.setData(data);
response.setCode(responseStatusEnum.getCode());
response.setStatus(responseStatusEnum.getStatus());
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<Map<String, T>> response(String key, T data, ResponseStatusEnum status) {
Map<String, T> map = new HashMap<>(16);
Response<Map<String, T>> response = Response.instance();
map.put(key, data);
response.setData(map);
response.setCode(status.getCode());
response.setStatus(status.getStatus());
response.setMessage(status.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> response(T data, Integer code, String status, String message) {
Response<T> response = Response.instance();
response.setData(data);
response.setCode(code);
response.setStatus(status);
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<Map<String, T>> response(String key, T data, Integer code, String status, String message) {
Map<String, T> map = new HashMap<>(16);
Response<Map<String, T>> response = Response.instance();
map.put(key, data);
response.setData(map);
response.setCode(code);
response.setStatus(status);
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> ok(T data) {
Response<T> response = Response.instance();
response.setData(data);
response.setCode(ResponseStatusEnum.SUCCESS.getCode());
response.setStatus(ResponseStatusEnum.SUCCESS.getStatus());
response.setMessage(ResponseStatusEnum.SUCCESS.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<Map<String, T>> ok(String key, T data) {
Map<String, T> map = new HashMap<>(16);
Response<Map<String, T>> response = Response.instance();
map.put(key, data);
response.setData(map);
response.setCode(ResponseStatusEnum.SUCCESS.getCode());
response.setStatus(ResponseStatusEnum.SUCCESS.getStatus());
response.setMessage(ResponseStatusEnum.SUCCESS.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> okMessage(String message) {
Response<T> response = Response.instance();
response.setData(null);
response.setCode(ResponseStatusEnum.SUCCESS.getCode());
response.setStatus(ResponseStatusEnum.SUCCESS.getStatus());
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> fail(T data) {
Response<T> response = Response.instance();
response.setData(data);
response.setCode(ResponseStatusEnum.FAIL.getCode());
response.setStatus(ResponseStatusEnum.FAIL.getStatus());
response.setMessage(ResponseStatusEnum.FAIL.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<Map<String, T>> fail(String key, T data) {
Map<String, T> map = new HashMap<>(16);
Response<Map<String, T>> response = Response.instance();
map.put(key, data);
response.setData(map);
response.setCode(ResponseStatusEnum.FAIL.getCode());
response.setStatus(ResponseStatusEnum.FAIL.getStatus());
response.setMessage(ResponseStatusEnum.FAIL.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> fail(ResponseStatusEnum responseStatusEnum, String message) {
return returnResponse(responseStatusEnum, message);
}
public static <T> Response<T> failMessage(String message) {
Response<T> response = Response.instance();
response.setData(null);
response.setCode(ResponseStatusEnum.FAIL.getCode());
response.setStatus(ResponseStatusEnum.FAIL.getStatus());
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> failMessage(Integer code, String message) {
return returnResponse(code, message);
}
public static <T> Response<T> error(T data) {
Response<T> response = Response.instance();
response.setData(data);
response.setCode(ResponseStatusEnum.ERROR.getCode());
response.setStatus(ResponseStatusEnum.ERROR.getStatus());
response.setMessage(ResponseStatusEnum.ERROR.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<Map<String, T>> error(String key, T data) {
Map<String, T> map = new HashMap<>(16);
Response<Map<String, T>> response = Response.instance();
map.put(key, data);
response.setData(map);
response.setCode(ResponseStatusEnum.ERROR.getCode());
response.setStatus(ResponseStatusEnum.ERROR.getStatus());
response.setMessage(ResponseStatusEnum.ERROR.getMessage());
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> error(ResponseStatusEnum responseStatusEnum, String message) {
return returnResponse(responseStatusEnum, message);
}
public static <T> Response<T> errorMessage(String message) {
Response<T> response = Response.instance();
response.setData(null);
response.setCode(ResponseStatusEnum.ERROR.getCode());
response.setStatus(ResponseStatusEnum.ERROR.getStatus());
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
public static <T> Response<T> errorMessage(Integer code, String message) {
return returnResponse(code, message);
}
public static <T> Response<T> okOrFail(T data, String message) {
if (null == data) {
return response(null, ResponseStatusEnum.FAIL, ResponseStatusEnum.FAIL.getMessage());
}
return response(data, ResponseStatusEnum.SUCCESS, message);
}
public static <T> Response<T> okOrFail(T data) {
if (null == data) {
return response(null, ResponseStatusEnum.FAIL);
}
return response(null, ResponseStatusEnum.SUCCESS);
}
private static <T> Response<T> returnResponse(ResponseStatusEnum responseStatusEnum, String message) {
Response<T> response = Response.instance();
response.setData(null);
response.setCode(responseStatusEnum.getCode());
response.setStatus(responseStatusEnum.getStatus());
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
private static <T> Response<T> returnResponse(Integer code, String message) {
Response<T> response = Response.instance();
response.setData(null);
response.setCode(code);
response.setStatus(ResponseStatusEnum.ERROR.getStatus());
response.setMessage(message);
response.setTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return response;
}
}
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
一些常用的响应状态枚举类:
/**
* @author Young Kbt
* @date 2022/4/30 15:19
* @note 响应状态枚举类
*/
public enum ResponseStatusEnum {
/**
* 规范响应体中的响应码和响应信息
*/
SUCCESS(HttpServletResponse.SC_OK, "success", "操作成功"),
/**
* 操作失败
*/
FAIL(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "fail", "操作失败"),
/**
* 操作错误
*/
ERROR(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "error", "操作错误"),
/**
* 请求未授权
*/
UN_AUTHORIZED(HttpServletResponse.SC_UNAUTHORIZED, "fail", "请求未授权"),
/**
* 客户端请求未授权
*/
CLIENT_UN_AUTHORIZED(HttpServletResponse.SC_UNAUTHORIZED, "fail", "客户端请求未授权"),
/**
* 404 没找到请求
*/
NOT_FOUND(HttpServletResponse.SC_NOT_FOUND, "fail", "404 没找到请求"),
/**
* 消息不能读取
*/
MSG_NOT_READABLE(HttpServletResponse.SC_BAD_REQUEST, "fail", "消息不能读取"),
/**
* 不支持当前请求方法
*/
METHOD_NOT_SUPPORTED(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "fail", "不支持当前请求方法"),
/**
* 不支持当前媒体类型
*/
MEDIA_TYPE_NOT_SUPPORTED(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "fail", "不支持当前媒体类型"),
/**
* 请求被拒绝
*/
REQ_REJECT(HttpServletResponse.SC_FORBIDDEN, "fail", "请求被拒绝"),
/**
* 服务器异常
*/
INTERNAL_SERVER_ERROR(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "fail", "服务器异常"),
/**
* 非法请求
*/
ILLEGALITY_REQUEST(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "fail", "非法请求"),
/**
* 缺少必要的请求参数
*/
PARAM_MISS(HttpServletResponse.SC_BAD_REQUEST, "fail", "缺少必要的请求参数"),
/**
* 请求参数类型错误
*/
PARAM_TYPE_ERROR(HttpServletResponse.SC_BAD_REQUEST, "fail", "请求参数类型错误"),
/**
* 请求参数绑定错误
*/
PARAM_BIND_ERROR(HttpServletResponse.SC_BAD_REQUEST, "fail", "请求参数绑定错误"),
/**
* 参数校验失败
*/
PARAM_VALID_ERROR(HttpServletResponse.SC_BAD_REQUEST, "fail", "参数校验失败");
private Integer code;
private String status;
private String message;
ResponseStatusEnum(Integer code, String status, String message) {
this.code = code;
this.status = status;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
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
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
如何使用呢?如下:
@RestController
public class UserController {
@GetMapping("/login")
public Response<String> login() {
return HttpResult.ok("login successfully!");
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Response
作为返回类型,指定返回的 data 是 String,然后用 HttpResult 的静态方法返回。
编辑此页 (opens new window)
更新时间: 2023/09/18, 16:34:13