RestUtil.java
8.69 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.huaheng.common.utils.restful;
import com.alibaba.fastjson.JSONObject;
import com.huaheng.framework.aspectj.ApiLogAspect;
import com.huaheng.pc.monitor.apilog.domain.ApiLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
/**
* 调用 Restful 接口 Util
*
* @author sunjianlei
*/
@Slf4j
public class RestUtil {
private static String domain = null;
public static String path = null;
public static String getPath() {
if (path == null) {
path = SpringContextUtils.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path");
}
return oConvertUtils.getString(path);
}
/**
* RestAPI 调用器
*/
private final static RestTemplate RT;
static {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(10 * 1000);
// 设置连接超时时间
requestFactory.setReadTimeout(10 * 1000);
// 设置读取超时时间
RT = new RestTemplate(requestFactory);
// 解决乱码问题
RT.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
}
public static RestTemplate getRestTemplate() {
return RT;
}
/**
* 发送 get 请求
*/
public static JSONObject get(String url) {
return getNative(url, null, null).getBody();
}
/**
* 发送 get 请求
*/
public static JSONObject get(String url, JSONObject variables) {
return getNative(url, variables, null).getBody();
}
/**
* 发送 get 请求
*/
public static JSONObject get(String url, JSONObject variables, JSONObject params) {
return getNative(url, variables, params).getBody();
}
/**
* 发送 get 请求,返回原生 ResponseEntity 对象
*/
public static ResponseEntity<JSONObject> getNative(String url, JSONObject variables, JSONObject params) {
return request(url, HttpMethod.GET, variables, params);
}
/**
* 发送 Post 请求
*/
public static JSONObject post(String url) {
return postNative(url, null, null).getBody();
}
/**
* 发送 Post 请求
*/
public static JSONObject post(String url, JSONObject params) {
return postNative(url, null, params).getBody();
}
/**
* 发送 Post 请求
*/
public static JSONObject post(String url, JSONObject variables, JSONObject params) {
return postNative(url, variables, params).getBody();
}
/**
* 发送 POST 请求,返回原生 ResponseEntity 对象
*/
public static ResponseEntity<JSONObject> postNative(String url, JSONObject variables, JSONObject params) {
return request(url, HttpMethod.POST, variables, params);
}
/**
* 发送 put 请求
*/
public static JSONObject put(String url) {
return putNative(url, null, null).getBody();
}
/**
* 发送 put 请求
*/
public static JSONObject put(String url, JSONObject params) {
return putNative(url, null, params).getBody();
}
/**
* 发送 put 请求
*/
public static JSONObject put(String url, JSONObject variables, JSONObject params) {
return putNative(url, variables, params).getBody();
}
/**
* 发送 put 请求,返回原生 ResponseEntity 对象
*/
public static ResponseEntity<JSONObject> putNative(String url, JSONObject variables, JSONObject params) {
return request(url, HttpMethod.PUT, variables, params);
}
/**
* 发送 delete 请求
*/
public static JSONObject delete(String url) {
return deleteNative(url, null, null).getBody();
}
/**
* 发送 delete 请求
*/
public static JSONObject delete(String url, JSONObject variables, JSONObject params) {
return deleteNative(url, variables, params).getBody();
}
/**
* 发送 delete 请求,返回原生 ResponseEntity 对象
*/
public static ResponseEntity<JSONObject> deleteNative(String url, JSONObject variables, JSONObject params) {
return request(url, HttpMethod.DELETE, null, variables, params, JSONObject.class, null);
}
/**
* 发送请求
*/
public static ResponseEntity<JSONObject> request(String url, HttpMethod method, JSONObject variables, JSONObject params) {
return request(url, method, getHeaderApplicationJson(), variables, params, JSONObject.class, null);
}
/**
* 发送POST请求(json,po)
*/
public static ResponseEntity<JSONObject> request_post(String url, String warehouseCode, Object JsonParam) {
HttpMethod method = HttpMethod.POST;
HttpHeaders Research = new HttpHeaders();
Research.setContentType(MediaType.valueOf("application/json;charset=UTF-8"));
return request(url, method, Research, null, JsonParam, JSONObject.class, warehouseCode);
}
/**
* 发送请求
*
* @param url 请求地址
* @param method 请求方式
* @param headers 请求头 可空
* @param variables 请求url参数 可空
* @param params 请求body参数 可空
* @param responseType 返回类型
* @return ResponseEntity<responseType>
*/
public static <T> ResponseEntity<T> request(String url, HttpMethod method, HttpHeaders headers, JSONObject variables, Object params, Class<T> responseType, String warehouseCode) {
log.info("sendSSLPost - {}", url);
String msg = "WMS请求发送成功,接收返回内容是空的";
ResponseEntity<T> exchange = null;
ApiLog log = null;
try {
if (StringUtils.isEmpty(url)) {
throw new RuntimeException("url 不能为空");
}
if (method == null) {
throw new RuntimeException("method 不能为空");
}
if (headers == null) {
headers = new HttpHeaders();
}
// 请求体
String body = "";
if (params != null) {
if (params instanceof JSONObject) {
body = ((JSONObject) params).toJSONString();
} else {
body = params.toString();
}
}
// 拼接 url 参数
if (variables != null) {
url += ("?" + asUrlVariables(variables));
}
// 发送请求
HttpEntity<String> request = new HttpEntity<>(body, headers);
String param = params + "";
//先初始化
log = ApiLogAspect.initApiLog(method.name(), url, param, headers, warehouseCode);
exchange = RT.exchange(url, method, request, responseType);
if (exchange != null) {
msg = String.valueOf(exchange.getBody());
}
} catch (Exception e) {
//错误信息写到日志
ApiLogAspect.setApiLogException(log, e);
e.printStackTrace();
} finally {
//写入数据库
ApiLogAspect.finishApiLog(log, headers, msg);
}
return exchange;
}
/**
* 获取JSON请求头
*/
public static HttpHeaders getHeaderApplicationJson() {
return getHeader(MediaType.APPLICATION_JSON_UTF8_VALUE);
}
/**
* 获取请求头
*/
public static HttpHeaders getHeader(String mediaType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(mediaType));
headers.add("Accept", mediaType);
return headers;
}
/**
* 将 JSONObject 转为 a=1&b=2&c=3...&n=n 的形式
*/
public static String asUrlVariables(JSONObject variables) {
Map<String, Object> source = variables.getInnerMap();
Iterator<String> it = source.keySet().iterator();
StringBuilder urlVariables = new StringBuilder();
while (it.hasNext()) {
String key = it.next();
String value = "";
Object object = source.get(key);
if (object != null) {
if (!StringUtils.isEmpty(object.toString())) {
value = object.toString();
}
}
urlVariables.append("&").append(key).append("=").append(value);
}
// 去掉第一个&
return urlVariables.substring(1);
}
}