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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.yanzuoguang.util.vo;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 接口之间的通讯结果
*
* @author 颜佐光
*/
public class ResponseResult<T> extends BaseVo {
private static final long serialVersionUID = -508702000019378459L;
/**
* 请求状态码
*/
@ApiModelProperty(value = "返回状态", notes = "00表示成功,其他状态表示失败,具体含义参照具体实现接口", required = true, example = "00")
private String code;
/**
* 服务消息
*/
@ApiModelProperty(value = "错误消息", notes = "提示错误消息等等", required = true, example = "查询成功")
private String message;
/**
* 返回数据
*/
@ApiModelProperty(value = "返回数据", notes = "返回的具体数据", required = true)
private T data;
/**
* 数据源,用于显示异常数据
*/
@ApiModelProperty(value = "异常数据", notes = "当抛出异常时的数据,通常和code进行组合", required = true)
private Object target;
/**
* 检查数据是否合法,不合法则抛出异常,合法则返回数据
*/
public T check() {
if (StringHelper.compare(code, ResultConstants.SUCCESS)) {
return this.data;
}
throw new CodeException(this.code, this.message, this.target);
}
/**
* 获取结果状态码
*
* @return
*/
public String getCode() {
return code;
}
/**
* 设置请求结果状态码
*
* @param code
*/
public void setCode(String code) {
this.code = code;
}
/**
* 获取结果消息
*
* @return
*/
public String getMessage() {
return message;
}
/**
* 设置结果消息
*
* @param message
*/
public void setMessage(String message) {
this.message = message;
}
/**
* 获取数据
*
* @return
*/
public T getData() {
return data;
}
/**
* 设置数据
*
* @param data
*/
public void setData(T data) {
this.data = data;
}
/**
* 获取数据源
*
* @return 数据源
*/
public Object getTarget() {
return target;
}
/**
* 设置数据源
*
* @param target
*/
public void setTarget(Object target) {
this.target = target;
}
/**
* 构造函数
*/
public ResponseResult() {
this(ResultConstants.SUCCESS, "操作成功");
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果消息
*/
public ResponseResult(String code, String message) {
this(code, message, null);
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果熊希
* @param data 结果
*/
public ResponseResult(String code, String message, T data) {
this(code, message, data, null);
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果熊希
* @param data 结果
*/
public ResponseResult(String code, String message, T data, Object target) {
this.code = code;
this.message = message;
this.data = data;
this.target = target;
}
/**
* 构造成功结果
*
* @param data 数据
* @param <T> 数据类型
* @return 一个请求成功的数据集合
*/
public static final <T extends Object> ResponseResult<T> result(T data) {
return result(data, false);
}
/**
* 构造成功结果,不允许为空
*
* @param data 数据
* @param <T> 数据类型
* @return 一个请求成功的数据集合
*/
public static final <T extends Object> ResponseResult<T> resultAllowNull(T data) {
return result(data, true);
}
/**
* 构造成功结果
*
* @param data 数据
* @param allowNull 允许为空
* @param <T> 数据类型
* @return 一个请求成功的数据集合
*/
public static final <T extends Object> ResponseResult<T> result(T data, boolean allowNull) {
ResponseResult<T> ret = new ResponseResult<T>();
ret.setData(data);
initDataStatus(ret, data, allowNull);
return ret;
}
protected static void initDataStatus(ResponseResult ret, Object data, boolean allowNull) {
if (!allowNull && StringHelper.isEmpty(data)) {
ret.setCode(ResultConstants.RESULT_EMPTY);
ret.setMessage("结果为空");
} else {
ret.setCode(ResultConstants.SUCCESS);
ret.setMessage("处理成功");
}
}
/**
* 处理错误
*
* @param target 错误源对象
* @return 处理的错误
*/
public static final ResponseResult error(Object target) {
return error(ResultConstants.UNKNOW_ERROR, "未知异常", target);
}
/**
* 处理错误
*
* @param code 错误码
* @param message 错误消息
* @return 处理的错误
*/
public static final ResponseResult error(String code, String message) {
return error(code, message, null);
}
/**
* 处理错误
*
* @param message 错误消息
* @param target 错误标记
* @return 处理的错误
*/
public static final ResponseResult error(String message, Object target) {
return error(ResultConstants.UNKNOW_ERROR, message, null);
}
/**
* 处理错误
*
* @param code 错误码
* @param message 错误消息
* @param target 错误标记
* @return 处理的错误
*/
public static final ResponseResult error(String code, String message, Object target) {
ResponseResult ret = new ResponseResult(code, message, null, target);
return ret;
}
}