# 统一封装接口返回(ApiResponse)
package com.warren.common.entity;
import com.warren.common.config.message.BusinessMessageResouseConfig;
import com.warren.common.utils.StringUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Map;
import java.util.Objects;
/**
* @PackageName org.sulotion.entity
* @ClassName Result
* Version v1.0
* @description 返回POJO类
*/
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@ApiModel(value = "ApiResponse", description = "ApiResponse")
public class ApiResponse<T> implements Serializable {
@ApiModelProperty(value = "code ")
private String code;
@ApiModelProperty(value = "message ")
private String message;
@ApiModelProperty(value = "info ")
private T info;
public static final String CODE_OK = "0000";
public static final String CODE_ERROR = "1111";
public static final String CODE_VALID = "0001";
public static final String CODE_EXPIRE = "0002";
public static final String NO_DATA = "0402";
public ApiResponse() {
}
public ApiResponse(String msg) {
this.code = CODE_VALID;
this.message = msg;
}
public ApiResponse(String code, String msg) {
this.code = code;
Map<String, String> msgPropertiesMap = BusinessMessageResouseConfig.msgPropertiesMap;
if(StringUtil.isNotEmpty(msgPropertiesMap.get(msg))) {
this.message = msgPropertiesMap.get(msg);
} else {
this.message = msg;
}
}
public static ApiResponse resultValid(String msg) {
ApiResponse responseResult = new ApiResponse();
responseResult.setCode(CODE_VALID);
responseResult.setMessage(StringUtil.isNotEmpty(BusinessMessageResouseConfig.msgPropertiesMap.get(msg))? BusinessMessageResouseConfig.msgPropertiesMap.get(msg) : msg);
return responseResult;
}
/**
* @MethodName: Result
* @Description: 普通返回
* @Param: String code, String msg,Object info
* @Return:
* @Author:
* @Date: 17:37
**/
public ApiResponse(String code, String msg, T info) {
this.code = code;
Map<String, String> msgPropertiesMap = BusinessMessageResouseConfig.msgPropertiesMap;
String msg_temp = msgPropertiesMap.get(msg);
if(StringUtil.isNotEmpty(msg_temp)) {
this.message = msg_temp;
} else {
this.message = msg;
}
this.info = info;
}
/**
* @MethodName: Result
* @Description: 占位符替换
* @Param: String code, String msg, Object info, List<String> params
* @Return:
* @Author:
* @Date: 17:36
**/
public ApiResponse(String code, String msg, T info, String[] params) {
this.code = code;
Map<String, String> msgPropertiesMap = BusinessMessageResouseConfig.msgPropertiesMap;
String temp = msgPropertiesMap.get(msg);
String[] msgList = StringUtil.splitString(temp, "%");
if (params.length + 1 == msgList.length) {
int k = 0;
String msgInfo = msgList[0];
for (int i = 1; i < msgList.length; i++) {
k = i - 1;
msgInfo += params[k]+ msgList[i];
}
this.message = msgInfo;
}
this.info = info;
}
public ApiResponse(String code, String msg, String[] params) {
this.code = code;
Map<String, String> msgPropertiesMap = BusinessMessageResouseConfig.msgPropertiesMap;
String temp = msgPropertiesMap.get(msg);
String[] msgList = StringUtil.splitString(temp, "%");
if (params.length + 1 == msgList.length) {
int k = 0;
String msgInfo = msgList[0];
for (int i = 1; i < msgList.length; i++) {
k = i - 1;
msgInfo += params[k]+ msgList[i];
}
this.message = msgInfo;
}
}
/**
* @MethodName: success
* @Description: 成功返回且无数据
* @Param:
* @Return:
* @Author:
* @Date: 17:37
**/
public static ApiResponse success() {
return new ApiResponse(CODE_OK, "msg0000");
}
/**
* @MethodName: success
* @Description: 成功返回有数据
* @Param:
* @Return:
* @Author:
* @Date: 17:37
**/
public static ApiResponse successObj(Object object) {
return new ApiResponse(CODE_OK, "msg0000",object);
}
/**
* @MethodName: error
* @Description: 错误返回且无数据
* @Param:
* @Return:
* @Author:
* @Date: 17:37
**/
public static ApiResponse error() {
return new ApiResponse(CODE_ERROR, "msg1111");
}
public static ApiResponse error(String errMsg) {
return new ApiResponse(CODE_ERROR, "errMsg",errMsg);
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
@Override
public String toString() {
return "Result{" +
"code='" + code + '\'' +
", message='" + message + '\'' +
", info=" + info +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ApiResponse)) {
return false;
}
ApiResponse responseResult = (ApiResponse) o;
return Objects.equals(getCode(), responseResult.getCode()) &&
Objects.equals(getMessage(), responseResult.getMessage()) &&
Objects.equals(getInfo(), responseResult.getInfo());
}
@Override
public int hashCode() {
return Objects.hash(getCode(), getMessage(), getInfo());
}
}
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
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
全量分析 阅读量: