From e38167e748ee30dd3317e6781f1b4c2067e8b8ca Mon Sep 17 00:00:00 2001 From: felord Date: Wed, 18 Aug 2021 16:31:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=20V2=20=E7=AD=BE=E5=90=8D=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../payment/wechat/v2/model/BaseModel.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v2/model/BaseModel.java b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v2/model/BaseModel.java index 1f6380f..ede9b42 100644 --- a/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v2/model/BaseModel.java +++ b/payment-spring-boot-autoconfigure/src/main/java/cn/felord/payment/wechat/v2/model/BaseModel.java @@ -21,8 +21,8 @@ package cn.felord.payment.wechat.v2.model; import cn.felord.payment.PayException; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.dataformat.xml.XmlMapper; @@ -56,6 +56,8 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.cert.CertificateException; +import java.util.TreeMap; +import java.util.stream.Collectors; /** * The type Base model. @@ -74,7 +76,8 @@ public abstract class BaseModel { XML_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL) // 属性使用 驼峰首字母小写 .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); - OBJECT_MAPPER.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) + OBJECT_MAPPER +// .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) .setSerializationInclusion(JsonInclude.Include.NON_NULL) .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); } @@ -166,13 +169,17 @@ public abstract class BaseModel { @SneakyThrows private String link(T t) { Assert.hasText(appSecret, "wechat pay appSecret is required"); - String link = OBJECT_MAPPER - .writer() - .writeValueAsString(t) - .replaceAll("\":\"", "=") - .replaceAll("\",\"", "&") - .replaceAll("\\\\\"", "\""); - return link.substring(2, link.length() - 2).concat("&key=").concat(this.appSecret); + String json = OBJECT_MAPPER + .writeValueAsString(t); + + TreeMap map = OBJECT_MAPPER.readValue(json, new TypeReference>() { + }); + + String query = map.entrySet() + .stream() + .map(entry -> entry.getKey().concat(entry.getValue())) + .collect(Collectors.joining("&")); + return query.concat("&key=").concat(this.appSecret); }