RestTemplateClient.java
7.28 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.topdraw.resttemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.config.ResponseStatus;
import com.topdraw.mq.consumer.UcEventBusIptv2ManagementUcEngine;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.mq.domain.SubscribeBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
@Slf4j
@Component
public class RestTemplateClient {
private static RestTemplate restTemplate;
private static String BASE_URL;
@Autowired
private Environment environment;
@PostConstruct
private void init() {
BASE_URL = environment.getProperty("api.baseUrl");
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// 设置连接超时
factory.setConnectTimeout(5000);
// 设置读取超时
factory.setReadTimeout(8000);
restTemplate = new RestTemplate(factory);
}
public JSONObject dealTask(DataSyncMsg dataSyncMsg) {
try {
String url = BASE_URL + "/uce/taskOperation/dealTask";
HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("content", JSON.toJSONString(dataSyncMsg));
log.info("request url is ==>> {} || param is ==>> {} ", url, objectObjectHashMap);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, objectObjectHashMap, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("处理普通权益任务(ApiUti.dealTask)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject unsubscribe(SubscribeBean subscribeBean) {
try {
String url = BASE_URL + "/uce/userOperation/unsubscribe";
String content = JSON.toJSONString(subscribeBean);
HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("content", content);
log.info("request url is ==>> {} || param is ==>> {} ", url, objectObjectHashMap);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, objectObjectHashMap, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("处理微信取关(ApiUti.unsubscribe)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject subscribe(SubscribeBean subscribeBean) {
try {
String url = BASE_URL + "/uce/userOperation/subscribe";
String content = JSON.toJSONString(subscribeBean);
HashMap<String, String> objectObjectHashMap = new HashMap<>();
objectObjectHashMap.put("content", content);
log.info("request url is ==>> {} || param is ==>> {} ", url, objectObjectHashMap);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, subscribeBean, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("处理微信关注(ApiUti.subscribe)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject addCollection(String content) {
try {
String url = BASE_URL + "/uce/userOperation/addCollection";
//处理接口调用 中文不显示问题
content = new String(Base64.getEncoder().encode(content.getBytes(StandardCharsets.UTF_8)));
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("添加观影记录(ApiUti.addCollection)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject deleteCollection(String content) {
try {
String url = BASE_URL + "/uce/userOperation/deleteCollection";
//处理接口调用 中文不显示问题
content = new String(Base64.getEncoder().encode(content.getBytes(StandardCharsets.UTF_8)));
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("删除一条观影记录(ApiUti.deleteCollection)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject deleteAllCollection(String content) {
try {
String url = BASE_URL + "/uce/userOperation/deleteAllCollection";
log.info("request url is ==>> {} || param is ==>> {} ", url, content);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("删除所有观影记录(ApiUti.deleteAllCollection)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
public JSONObject dealViewRecord(String content) {
try {
String url = BASE_URL + "/uce/userOperation/addCollection";
//处理接口调用 中文不显示问题
content = new String(Base64.getEncoder().encode(content.getBytes(StandardCharsets.UTF_8)));
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, content, String.class);
log.info("response ==>> {}", responseEntity);
return getParseResponseResult(responseEntity);
} catch (Exception e) {
log.error("处理观影记录(ApiUti.dealViewRecord)信息时出现异常,cause ==>> {}", e.getMessage());
}
return null;
}
/**
*
* @param responseEntity
* @return
*/
private static JSONObject getParseResponseResult(ResponseEntity<String> responseEntity) {
JSONObject resultSet = null;
if (responseEntity.getStatusCode().is2xxSuccessful()) {
String entityBody = responseEntity.getBody();
JSONObject jsonObject = JSONObject.parseObject(entityBody);
if (jsonObject.getInteger("businessCode").equals(ResponseStatus.OK)) {
resultSet = jsonObject.getJSONArray("resultSet").getJSONObject(0);
}
}
log.info("result ==>> {}", resultSet);
return resultSet;
}
}