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
|
import org.springframework.beans.factory.annotation.Value;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.entity.ContentType;
class MessageRestController {
@Value("${restService_url: default_url}")
private String url;
@RequestMapping("/message")
String getMessage() {
CloseableHttpClient httpClient = HttpClients.createDefault();
String body;
if (param instanceof String) {
body = (String) param;
} else {
body = JSONObject.toJSONString(param);
}
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 添加http headers
if (headers != null && headers.size() > 0) {
for (String key : headers.keySet()) {
httpPost.addHeader(key, headers.get(key));
}
StringEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
}
|