我们已经在 rest 系统中实现了商品分类的API,那么接下来我们就可以在 portal 系统中请求 rest 中的分类数据,我们可以使用 httpclient 进行不同系统之间的请求,接着将获取到的数据用 freemarker 进行展示。

创建 HttpClient

在 common 模块中依赖相关jar包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>

创建 Httpclient 工具类

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
162
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


/**
* HttpClientUtil
*/
public class HttpClientUtil {

// 连接主机超时(30s)
public static final int HTTP_CONNECT_TIMEOUT_30S = 30 * 1000;

// 从主机读取数据超时(3min)
public static final int HTTP_READ_TIMEOUT_3MIN = 180 * 1000;

/**
* httpPost
*/
public static String httpPost(String url, String jsonParam) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);

// 设置请求头和请求参数
if (null != jsonParam && !jsonParam.isEmpty()) {
StringEntity entity = new StringEntity(jsonParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}

// 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpPost.setConfig(requestConfig);

// 发送请求
CloseableHttpResponse response = httpclient.execute(httpPost);

// 获取返回内容
try {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
EntityUtils.consume(entity); // 此句关闭了流
return str;
} finally {
response.close();
}
}

/**
* httpPost get Cookies
*/
public static Map<String, Object> httpPostGetCookies(String url, String jsonParam) throws
IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);

// 设置请求头和请求参数
if (null != jsonParam && !jsonParam.isEmpty()) {
StringEntity entity = new StringEntity(jsonParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}

// 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpPost.setConfig(requestConfig);

// 发送请求
CloseableHttpResponse response = httpclient.execute(httpPost);

// 获取返回内容
try {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
EntityUtils.consume(entity); // 此句关闭了流

// 获取数据内容
Map<String, Object> map = new HashMap<String, Object>();
map.put("result", str);

// 获取返回到额Cookies
Header[] headers = response.getHeaders("Set-Cookie");
map.put("cookies", headers);

return map;
} finally {
response.close();
}
}

/**
* httpGet
*/
public static String httpGet(String url) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);

// 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpGet.setConfig(requestConfig);

// 发送请求
CloseableHttpResponse response = httpclient.execute(httpGet);

// 获取返回内容
try {
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity);
return strResult;
} finally {
response.close();
}
}

/**
* httpGet with Cookies
*/
public static String httpGetWithCookies(String url, Header[] headers) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);

// 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpGet.setConfig(requestConfig);

// 设置请求头
if (headers != null && headers.length > 0) {
httpGet.setHeaders(headers);
}

// 发送请求
CloseableHttpResponse response = httpclient.execute(httpGet);

// 获取返回内容
try {
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity);
return strResult;
} finally {
response.close();
}
}

}

添加 source 配置文件

1
2
# REST
REST_URL = http://127.0.0.1:8088/rest/

添加配置路径文件

1
2
3
4
5
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location">
<value>classpath:resource/resource.properties</value>
</property>
</bean>

Service 的具体实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Service
public class CategoriesServiceImpl implements CategoriesService {

@Value("${REST_URL}")
private String REST_URL;
@Value("${CATEGORY_LIST}")
private String CATEGORY_LIST;

@Override
public Res_Categories getCategories() {
try {
String jsonResutlt = HttpClientUtil.httpGet(REST_URL + CATEGORY_LIST);
Res_Categories res_categories = GsonUtil.fromJson(Res_Categories.class, jsonResutlt);
return res_categories;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}