ApiResponse.java
4.13 KB
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
package com.yoho.rfid.util;
import com.alibaba.fastjson.JSON;
import com.yoho.core.common.utils.MD5;
import com.yoho.error.GatewayError;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import java.util.ArrayList;
public class ApiResponse {
private static String DEFAULT_MSG = "操作成功";
private static int DEFAULT_CODE = 200;
private static final String MD5_SALT = "fd4ad5fcsa0de589af23234ks1923ks";
private int code;
private String message;
private String md5;
//如果客户端判断有这个,则校验MD5
private String alg = "SALT_MD5";
private Object data;
public ApiResponse() {
this(200, DEFAULT_MSG, null);
}
public ApiResponse(int code, String message, Object data) {
this.code = code;
if (StringUtils.isNotEmpty(message)) {
this.message = message;
}
this.data = data;
}
public String getAlg() {
return alg;
}
public void setAlg(String alg) {
this.alg = alg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getMd5() {
return md5;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
/**
* 构造响应。 使用方式:
* <p/>
* <pre>
* ApiResponse.ApiResponseBuilder builder = new ApiResponse.ApiResponseBuilder();
* ApiResponse apiResponse = builder.code(200).message("coupons total").data(new Total("0")).build();
* </pre>
*/
public static class ApiResponseBuilder {
ApiResponse apiResponse;
public ApiResponseBuilder() {
apiResponse = new ApiResponse();
}
/**
* 设置错误码。默认200
*
* @param code 错误码
* @return ApiResponseBuilder
*/
public ApiResponseBuilder code(int code) {
apiResponse.code = code;
return this;
}
/**
* 设置消息。默认[操作成功]
*
* @param message 错误消息
* @return ApiResponseBuilder
*/
public ApiResponseBuilder message(String message) {
apiResponse.message = message;
return this;
}
/**
* 从Errorcode中设置错误码和错误消息
*
* @param gatewayError
* @return
*/
public ApiResponseBuilder code(GatewayError gatewayError) {
apiResponse.code = gatewayError.getCode();
apiResponse.message = gatewayError.getMessage();
return this;
}
/**
* 设置响应的具体内容
*
* @param data 响应的具体内容
* @return 内容
*/
public ApiResponseBuilder data(Object data) {
apiResponse.data = data;
return this;
}
/**
* 构造响应
*
* @return 响应
*/
public ApiResponse build() {
//参数校验, 并且设置默认值
if (this.apiResponse.code <= 0) {
this.apiResponse.code = DEFAULT_CODE;
}
if (StringUtils.isEmpty(apiResponse.message)) {
this.apiResponse.message = DEFAULT_MSG;
}
//构造JSON
apiResponse.md5 = getMd5();
return apiResponse;
}
//计算MD5
private String getMd5() {
if (this.apiResponse.data == null) {
this.apiResponse.data = new ArrayList<>(0);
}
String json = JSON.toJSONString(this.apiResponse.data); // []
return MD5.md5(MD5_SALT+":"+json);
}
}
}