|
@@ -4,8 +4,9 @@ import org.springframework.http.*;
|
4
|
4
|
import org.springframework.stereotype.Component;
|
5
|
5
|
import org.springframework.web.client.RestTemplate;
|
6
|
6
|
|
7
|
|
-import java.util.Arrays;
|
8
|
|
-import java.util.Map;
|
|
7
|
+import java.io.UnsupportedEncodingException;
|
|
8
|
+import java.net.URLDecoder;
|
|
9
|
+import java.util.*;
|
9
|
10
|
|
10
|
11
|
@Component
|
11
|
12
|
public class HttpUtils {
|
|
@@ -14,7 +15,12 @@ public class HttpUtils {
|
14
|
15
|
HttpHeaders headers = new HttpHeaders();
|
15
|
16
|
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
16
|
17
|
HttpEntity<String> entity = new HttpEntity<String>(headers);
|
17
|
|
- ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
|
|
18
|
+
|
|
19
|
+ Map<String, Object> urlAndParams = parseURL(url);
|
|
20
|
+ String formatURL = (String) urlAndParams.get("url");
|
|
21
|
+ Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
|
|
22
|
+
|
|
23
|
+ ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.GET, entity, String.class, params);
|
18
|
24
|
return resp.getBody();
|
19
|
25
|
}
|
20
|
26
|
|
|
@@ -26,7 +32,56 @@ public class HttpUtils {
|
26
|
32
|
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
|
27
|
33
|
headers.setAll(header);
|
28
|
34
|
HttpEntity<String> entity = new HttpEntity<String>(body, headers);
|
29
|
|
- ResponseEntity<String> resp = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
|
|
35
|
+
|
|
36
|
+ Map<String, Object> urlAndParams = parseURL(url);
|
|
37
|
+ String formatURL = (String) urlAndParams.get("url");
|
|
38
|
+ Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
|
|
39
|
+
|
|
40
|
+ ResponseEntity<String> resp = restTemplate.exchange(formatURL, HttpMethod.POST, entity, String.class, params);
|
30
|
41
|
return resp.getBody();
|
31
|
42
|
}
|
|
43
|
+
|
|
44
|
+ // RestTemplate 不能正常的处理 querystring, 必须按照约定的方式处理
|
|
45
|
+ private Map<String, Object> parseURL(String url) {
|
|
46
|
+ Map<String, Object> res = new HashMap<>();
|
|
47
|
+
|
|
48
|
+ String[] parts = url.split("\\?");
|
|
49
|
+ String baseURL = parts[0];
|
|
50
|
+
|
|
51
|
+ if (parts.length < 2) {
|
|
52
|
+ res.put("url", baseURL);
|
|
53
|
+ res.put("params", new HashMap<>());
|
|
54
|
+ return res;
|
|
55
|
+ }
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+ String[] fields = parts[1].split("&");
|
|
59
|
+ Map<String, String> params = new HashMap<>();
|
|
60
|
+ List<String> searchList = new ArrayList<>();
|
|
61
|
+ for (String field : fields) {
|
|
62
|
+ String[] pairs = field.split("=");
|
|
63
|
+ if (2 != pairs.length) {
|
|
64
|
+ continue;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ try {
|
|
68
|
+ String key = URLDecoder.decode(pairs[0].trim(), "UTF-8");
|
|
69
|
+ String val = URLDecoder.decode(pairs[1].trim(), "UTF-8");
|
|
70
|
+
|
|
71
|
+ // 转化为 "foo={foo}" 这种字符串
|
|
72
|
+ searchList.add(String.format("%s={%s}", key, key));
|
|
73
|
+ // 把 foo 对应的值存储起来
|
|
74
|
+ params.put(key, val);
|
|
75
|
+ } catch (UnsupportedEncodingException e) {
|
|
76
|
+ e.printStackTrace();
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ // url 格式类似 http://foo.org/bar?name={name}
|
|
81
|
+ String formatURL = baseURL + "?" + String.join("&", searchList);
|
|
82
|
+ res.put("url", formatURL);
|
|
83
|
+ res.put("params", params);
|
|
84
|
+
|
|
85
|
+ return res;
|
|
86
|
+ }
|
32
|
87
|
}
|