Browse Source

http get header丢失问题修复

andy 9 months ago
parent
commit
2432607536

+ 1 - 1
mainFactory/pom.xml

@@ -11,7 +11,7 @@
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <artifactId>mainFactory</artifactId>
-    <version>1.0-SNAPSHOT</version>
+    <version>2.0.0</version>
     <properties>
         <maven.compiler.source>17</maven.compiler.source>
         <maven.compiler.target>17</maven.compiler.target>

+ 12 - 5
mainFactory/src/main/java/org/bfkj/protocol/WebAPI.java

@@ -2,10 +2,9 @@ package org.bfkj.protocol;
 
 import org.bfkj.utils.LogUtils;
 import org.bfkj.utils.MapTools;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
+import org.springframework.http.*;
 import org.springframework.http.converter.StringHttpMessageConverter;
+import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 
 import java.nio.charset.StandardCharsets;
@@ -25,6 +24,7 @@ public class WebAPI {
      * webapi 执行入口函数
      */
     public Map<String, Object> execWebApi(Object header, String method, Object defaultBody, String connectConfig) {
+        
         Map<String, Object> connectConfigMaps = (Map<String, Object>) MapTools.strToObj(connectConfig);
         Map<String, Object> restTemplateResult = initWebApiParams(connectConfigMaps); // 通过连接配置 获取restTemplate
 
@@ -41,7 +41,9 @@ public class WebAPI {
                 }
             }
         }
-        if (MapTools.isNotBlank(method)) webapiMethod = method.toLowerCase();
+        if (MapTools.isNotBlank(method)) {
+            webapiMethod = method.toLowerCase();
+        }
         try {
             List<Map<String, Object>> returnList = new ArrayList<>();
             if (defaultBody instanceof List<?> tempList) {
@@ -62,7 +64,12 @@ public class WebAPI {
             HttpEntity<Object> request = new HttpEntity<>(defaultBody, webApiHeader);
             String responseEntity = null;
             switch (webapiMethod) {
-                case "get" -> responseEntity = restTemplate.getForObject(webapiURL, String.class, request);
+                case "get" -> {
+                    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(webApiHeader);
+                    ResponseEntity<String> response =
+                            restTemplate.exchange(webapiURL, HttpMethod.GET, entity, String.class);
+                    responseEntity = response.getBody();
+                }
                 case "put" -> restTemplate.put(webapiURL, request);
                 case "delete" -> restTemplate.delete(webapiURL, request);
                 default -> responseEntity = restTemplate.postForObject(webapiURL, request, String.class);

+ 52 - 0
mainFactory/src/test/java/org/bfkj/protocol/WebAPITest.java

@@ -2,7 +2,11 @@ package org.bfkj.protocol;
 
 import org.junit.Test;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 public class WebAPITest {
@@ -26,4 +30,52 @@ public class WebAPITest {
 						  "body":{"appid": "wx95c5de88bftsegs","appSecret": "af1f4b97b4e13f27a"}}""");
 		System.out.println(stringObjectMap);
 	}
+ 
+	@Test
+    public void testTestExecWebApi() throws JsonProcessingException {
+	    var data = """
+				[
+					{
+						"dataContent": "1",
+						"currentUser": [],
+						"dataObjectId": "17189601729410009997010301",
+						"event": "1"
+					},
+					{
+						"returnData": {
+							"username": "camsg",
+							"password": "camsg1029;c"
+						},
+						"code": "0",
+						"execTime": 598,
+						"library_id": "200"
+					},
+					{
+						"code": "0",
+						"library_id": "201",
+						"returnData": "{\\"code\\":\\"000\\",\\"msg\\":\\"成功!\\",\\"data\\":{\\"token\\":\\"DATAac7a60fa329f4eaa85647f1134e96a50\\"}}",
+						"execTime": 371
+					},
+					{
+						"returnData": {
+							"header": {
+								"token": "DATAac7a60fa329f4eaa85647f1134e96a50"
+							},
+							"method": "get"
+						},
+						"code": "0",
+						"execTime": 66,
+						"library_id": "202"
+					}
+				]
+				""";
+	    ObjectMapper mapper = new ObjectMapper();
+	    List value = mapper.readValue(data, List.class);
+	    
+	    Map<String, Object> stringObjectMap =
+			    new WebAPI().execWebApi(((Map) ((Map) value.get(3)).get("returnData")).get("header"),
+					    "GET",
+					    null, "{\"url\":\"https://bts.travelsky.com/gateway/openApi/pullService/getData/bsm/v1\",\"headers\":{},\"method\":\"get\"}");
+	    System.out.println(stringObjectMap);
+    }
 }