Przeglądaj źródła

bug: fix resttemplate bug

张延森 3 lat temu
rodzic
commit
cf5751500f

+ 1
- 1
deploy/bootstrap Wyświetl plik

@@ -2,4 +2,4 @@
2 2
 #
3 3
 #
4 4
 
5
-java -jar ./colmo-1.0.1.jar
5
+java -jar ./colmo-1.0.3.jar

+ 1
- 1
deploy/s.yml Wyświetl plik

@@ -15,7 +15,7 @@ services:
15 15
         name: colmo-service
16 16
         description: 'colmo h5'
17 17
         ossBucket: yz-serverless
18
-        ossKey: dianyang/colmo-1.0.1.zip
18
+        ossKey: dianyang/colmo-1.0.3.zip
19 19
         handler: 'com.yunzhi.dianyang.SpringApplication::main'
20 20
         memorySize: 1024
21 21
         timeout: 30

+ 1
- 1
pom.xml Wyświetl plik

@@ -10,7 +10,7 @@
10 10
 	</parent>
11 11
 	<groupId>com.yunzhi</groupId>
12 12
 	<artifactId>dianyang</artifactId>
13
-	<version>1.0.1</version>
13
+	<version>1.0.3</version>
14 14
 	<name>colmo</name>
15 15
 	<description>colmo</description>
16 16
 

+ 59
- 4
src/main/java/com/yunzhi/dianyang/common/HttpUtils.java Wyświetl plik

@@ -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
 }

+ 2
- 1
src/main/java/com/yunzhi/dianyang/controller/WxMpController.java Wyświetl plik

@@ -22,7 +22,8 @@ public class WxMpController extends BaseController {
22 22
     @ApiOperation("获取JSSDK相关")
23 23
     @GetMapping("/wx/jssdk")
24 24
     public ResponseBean jssdk(@RequestParam("url") String url) throws Exception {
25
-        String json = httpUtils.get(String.format(jssdkAPI, url));
25
+        String json = httpUtils.get(String.format(jssdkAPI, StringUtils.urlEncode(url)));
26
+//        String json = httpUtils.get(String.format(jssdkAPI, url));
26 27
         JSONObject jsonObject = JSONUtil.parseObj(json);
27 28
 
28 29
         if (jsonObject.getInt("code") == ResponseBean.CODE_SUCCESS) {