RestClient.java
3.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
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
package com.cjs.site.util.net;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.cjs.site.util.lang.JsonUtil;
/**
* Rest接口客户端工具类
*
* @author tongyufu
*
*/
@Component
public class RestClient extends RestTemplate implements RestOperation {
public RestClient() {
//连接超时
((SimpleClientHttpRequestFactory) this.getRequestFactory()).setConnectTimeout(20000);
//读取超时
((SimpleClientHttpRequestFactory) this.getRequestFactory()).setReadTimeout(20000);
ClientHttpRequestInterceptor interceptor = new RestClientHttpRequestInterceptor();
this.setInterceptors(Collections.singletonList(interceptor));
}
@SuppressWarnings("unchecked")
public <T> List<T> getForList(String url, Class<T> elementType, Object... urlVariables) {
String response = super.getForObject(url, String.class, urlVariables);
return JsonUtil.fromJson(response, List.class, elementType);
}
@SuppressWarnings("unchecked")
public <T> List<T> getForList(String url, Class<T> elementType, Map<String, ?> urlVariables) {
String response = super.getForObject(url, String.class, urlVariables);
return JsonUtil.fromJson(response, List.class, elementType);
}
@SuppressWarnings("unchecked")
public <T> List<T> postForList(String url, Object request, Class<T> elementType,
Object... urlVariables) {
String response = super.postForObject(url, request, String.class, urlVariables);
return JsonUtil.fromJson(response, List.class, elementType);
}
@SuppressWarnings("unchecked")
public <T> List<T> postForList(String url, Object request, Class<T> elementType,
Map<String, ?> urlVariables) {
String response = super.postForObject(url, request, String.class, urlVariables);
return JsonUtil.fromJson(response, List.class, elementType);
}
/**
* 使用POST请求发送JSON字符串
* @param url 请求路径
* @param request 请求体
* @param responseType 返回值类型
* @return
*/
public <T> T postForJson(String url, String request, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json", HttpUtil.CHARSET));
HttpEntity<String> entity = new HttpEntity<String>(request, headers);
String response = super.exchange(url, HttpMethod.POST, entity, String.class).getBody();
return JsonUtil.fromJson(response, responseType);
}
/**
* 使用POST请求发送JSON字符串
* @param url 请求路径
* @param request 请求体
* @param responseType 返回值类型
* @return
*/
public <L extends Collection<E>, E> L postForJson(String url, String request,
Class<L> collectionClass,
Class<E> elementClass) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType("application", "json", HttpUtil.CHARSET));
HttpEntity<String> entity = new HttpEntity<String>(request, headers);
String response = super.exchange(url, HttpMethod.POST, entity, String.class).getBody();
return JsonUtil.fromJson(response, collectionClass, elementClass);
}
}