Gb1400RestTemplate.java
1.81 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
package com.viontech.config;
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.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* @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;
}
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;
}
}