瀏覽代碼

fix: 用 Map.class 接收 API 响应,通过 valueToTree 转 JsonNode

Gson 可正常反序列化 Map,避免 String.class 触发的 MalformedMessageException

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline 2 周之前
父節點
當前提交
723f05558f
共有 1 個文件被更改,包括 28 次插入20 次删除
  1. 28 20
      haha-service/src/main/java/com/haha/service/payment/payscore/impl/WxPayScoreStrategy.java

+ 28 - 20
haha-service/src/main/java/com/haha/service/payment/payscore/impl/WxPayScoreStrategy.java

@@ -141,10 +141,11 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
 
             // 解析响应
-            JsonNode respJson = response.getServiceResponse();
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
 
             String outOrderNo = getJsonString(respJson, "out_order_no");
@@ -155,7 +156,7 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
             result.setPackageStr(packageStr);
 
             Map<String, Object> rawData = new HashMap<>();
-            rawData.put("response", respJson.toString());
+            rawData.put("response", responseMap);
             result.setRawData(rawData);
 
             log.info("[微信支付分] 创建订单成功 - 订单号: {}, 状态: {}, package: {}", outOrderNo, state, packageStr);
@@ -265,12 +266,13 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
 
             // 解析响应体
-            JsonNode respJson = response.getServiceResponse();
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
-            if (respJson != null && respJson.size() > 0) {
+            if (responseMap != null && !responseMap.isEmpty()) {
                 String outOrderNo = getJsonString(respJson, "out_order_no");
                 String state = getJsonString(respJson, "state");
                 log.info("[微信支付分] 完结订单成功 - 订单号: {}, 状态: {}", outOrderNo, state);
@@ -326,12 +328,13 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
 
             // 解析响应体
-            JsonNode respJson = response.getServiceResponse();
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
-            if (respJson != null && respJson.size() > 0) {
+            if (responseMap != null && !responseMap.isEmpty()) {
                 String outOrderNo = getJsonString(respJson, "out_order_no");
                 String state = getJsonString(respJson, "state");
                 log.info("[微信支付分] 取消订单成功 - 订单号: {}, 状态: {}", outOrderNo, state);
@@ -382,10 +385,11 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
 
             // 解析响应
-            JsonNode respJson = response.getServiceResponse();
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
 
             String respOutOrderNo = getJsonString(respJson, "out_order_no");
@@ -400,7 +404,7 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
             }
 
             Map<String, Object> rawData = new HashMap<>();
-            rawData.put("response", respJson.toString());
+            rawData.put("response", responseMap);
             result.setRawData(rawData);
 
             log.info("[微信支付分] 查询订单成功 - 订单号: {}, 状态: {}, 金额: {}元", 
@@ -468,8 +472,9 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
-            JsonNode respJson = response.getServiceResponse();
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
             String outOrderNo = getJsonString(respJson, "out_order_no");
             String state = getJsonString(respJson, "state");
@@ -521,8 +526,9 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
-            JsonNode respJson = response.getServiceResponse();
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
             String outOrderNo = getJsonString(respJson, "out_order_no");
             String state = getJsonString(respJson, "state");
@@ -572,8 +578,9 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
-            JsonNode respJson = response.getServiceResponse();
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
             String outOrderNo = getJsonString(respJson, "out_order_no");
             String refundId = getJsonString(respJson, "refund_id");
@@ -618,8 +625,9 @@ public class WxPayScoreStrategy implements PayScoreStrategy {
                     .addHeader("Accept", MediaType.APPLICATION_JSON.getValue())
                     .build();
 
-            HttpResponse<JsonNode> response = wxPayHttpClient.execute(httpRequest, JsonNode.class);
-            JsonNode respJson = response.getServiceResponse();
+            HttpResponse<Map> response = wxPayHttpClient.execute(httpRequest, Map.class);
+            Map<String, Object> responseMap = response.getServiceResponse();
+            JsonNode respJson = objectMapper.valueToTree(responseMap);
 
             String status = getJsonString(respJson, "status");