HttpRestClient.java
6.47 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
package com.ui.http;
import com.ui.config.SystemConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
public class HttpRestClient {
private static final Logger logger = LoggerFactory
.getLogger(HttpRestClient.class);
private RestTemplate restTemplate;
private SystemConfig systemConfig;
public <T> T defaultPost(String uri, Object request, Class<T> responseType) {
try {
return restTemplate.postForObject(systemConfig.getServiceUrl() + uri, request,responseType);
} catch (Exception e) {
logger.error("postForObject failed!url:"+uri, e);
return null;
}
}
public <T> T defaultPostJson(String uri, String requestJson,Class<T> responseType) {
try {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
return restTemplate.postForObject(systemConfig.getServiceUrl() + uri, entity, responseType);
} catch (Exception e) {
logger.error("postForObject failed!url:"+uri, e);
return null;
}
}
public <T> T defaultGet(String uri, Class<T> responseType,Map<String, ?> map) {
try {
StringBuilder url = new StringBuilder(uri);
if(map != null){
url.append("?");
for (Map.Entry<String, ?> entry : map.entrySet()) {
url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
return restTemplate.getForObject(systemConfig.getServiceUrl() + url.toString().substring(0, url.length()-1), responseType);
}
return restTemplate.getForObject(systemConfig.getServiceUrl() + uri, responseType);
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public <T> T exchangeForget(String uri, ParameterizedTypeReference<T> responseType,Map<String, ?> map) {
try {
StringBuilder url = new StringBuilder(uri);
if(map != null){
url.append("?");
for (Map.Entry<String, ?> entry : map.entrySet()) {
url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
HttpEntity<T> response = restTemplate.exchange(systemConfig.getServiceUrl() + url.toString().substring(0, url.toString().length()-1), HttpMethod.GET, null, responseType);
return response.getBody();
}
return restTemplate.exchange(systemConfig.getServiceUrl() + uri, HttpMethod.GET, null, responseType).getBody();
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public <T> T exchangeForpost(String uri, ParameterizedTypeReference<T> responseType,Object requst) {
try {
HttpEntity httpEntity = new HttpEntity(requst);
return restTemplate.exchange(systemConfig.getServiceUrl() + uri, HttpMethod.POST, httpEntity, responseType).getBody();
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public <T> T exchangeForPostOtherSystem(String uri, ParameterizedTypeReference<T> responseType,Object requst) {
try {
HttpEntity httpEntity = new HttpEntity(requst);
return restTemplate.exchange( uri, HttpMethod.POST, httpEntity, responseType).getBody();
} catch (Exception e) {
logger.error("exchangeForPostOtherSystem failed!url: "+uri, e);
return null;
}
}
public <T> T otherOxchangeForget(String uri, ParameterizedTypeReference<T> responseType,Map<String, ?> map) {
try {
StringBuilder url = new StringBuilder(uri);
if(map != null){
url.append("?");
for (Map.Entry<String, ?> entry : map.entrySet()) {
url.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
HttpEntity<T> response = restTemplate.exchange( url.toString().substring(0, url.toString().length()-1), HttpMethod.GET, null, responseType);
return response.getBody();
}
return restTemplate.exchange( uri, HttpMethod.GET, null, responseType).getBody();
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public <T> T get(String url, Class<T> responseType,Map<String, ?> map) {
try {
StringBuilder uri = new StringBuilder(url);
if(map != null){
uri.append("?");
for (Map.Entry<String, ?> entry : map.entrySet()) {
uri.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
return restTemplate.getForObject(uri.toString().substring(0, uri.length()-1), responseType);
}
return restTemplate.getForObject(url, responseType);
} catch (Exception e) {
logger.error("getForObject failed!url: "+url, e);
return null;
}
}
public <T> T post(String url, Object request, Class<T> responseType) {
try {
return restTemplate.postForObject(url, request,responseType);
} catch (Exception e) {
logger.error("postForObject failed!url:"+url, e);
return null;
}
}
public <T> T defaultGet(String uri, Class<T> responseType) {
try {
return restTemplate.getForObject(systemConfig.getServiceUrl() + uri, responseType);
} catch (Exception e) {
logger.error("getForObject failed!url: "+uri, e);
return null;
}
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public void setSystemConfig(SystemConfig systemConfig) {
this.systemConfig = systemConfig;
}
}