Gb1400RestTemplate.java
3.96 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
package com.viontech.config;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
/**
* @author vion
**/
@Component
@Slf4j
public class Gb1400RestTemplate {
@Autowired
private RestTemplate restTemplate;
@Autowired
private Gb1400Config gb1400Config;
public String doPost(String userIdentify, String uri, String content) {
HttpHeaders httpHeaders = setHeader(userIdentify);
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
String url = "http://" + gb1400Config.getIpPort() + uri;
HttpEntity<String> entity = new HttpEntity<>(content, httpHeaders);
if (content.length() > 500) {
// log.info("1400:{} data docking,params:{}", uri, content.substring(0, 500));
log.info("1400:{} data docking,params:{}", uri, content);
} else {
log.info("1400:{} data docking,params:{}", uri, content);
}
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, entity, String.class);
String result = responseEntity.getBody();
log.info("1400:{} data docking,result:{}", uri, result);
return result;
}
public String doGet(String userIdentify, String uri, Map params) {
String paramsStr = JSONObject.toJSONString(params);
HttpHeaders httpHeaders = setHeader(userIdentify);
HttpEntity<String> entity = new HttpEntity<>(paramsStr, httpHeaders);
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
String url = "http://" + gb1400Config.getIpPort() + uri;
if (params.containsKey("NotificationID")) {
url = url + "?NotificationID=" + params.get("NotificationID");
}
if (params.containsKey("SubscribeID")) {
if (url.indexOf("?") > 0) {
url = url + "&SubscribeID=" + params.get("SubscribeID");
} else {
url = url + "?SubscribeID=" + params.get("SubscribeID");
}
}
log.info("1400:{} data doGet,params:{}", url, paramsStr);
// ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class, params);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, new HashMap<>());
String result = responseEntity.getBody();
log.info("1400:{} data doGet,result:{}", uri, result);
return result;
}
public String doDelete(String userIdentify, String uri, Map params) {
String paramsStr = JSONObject.toJSONString(params);
HttpHeaders httpHeaders = setHeader(userIdentify);
HttpEntity<String> entity = new HttpEntity<>(paramsStr, httpHeaders);
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
String url = "http://" + gb1400Config.getIpPort() + uri;
log.info("1400:{} data doDelete,params:{}", uri, paramsStr);
// restTemplate.delete(url, params);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class, new HashMap<>());
String result = responseEntity.getBody();
log.info("1400:{} data doDelete,result:{}", uri, result);
return result;
}
private HttpHeaders setHeader(String userIdentify) {
HttpHeaders headers = new HttpHeaders();
headers.add("User-Identify", userIdentify);
headers.add("Content-Type", "application/VIID+JSON;charset=utf-8");
// String currentTime = DateUtil.getCurrentTime(DateUtil.TIMESTAMP_NO_DELIMITER);
// headers.add("User-SendTime", currentTime);
return headers;
}
}