diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResult.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResult.java index 166a3348c..3b5107276 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResult.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResult.java @@ -1,16 +1,19 @@ package com.github.binarywang.wxpay.bean.profitsharing; import com.github.binarywang.wxpay.bean.result.BaseWxPayResult; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.annotations.SerializedName; +import com.google.gson.reflect.TypeToken; import com.thoughtworks.xstream.annotations.XStreamAlias; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.w3c.dom.Document; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import java.io.Serializable; +import java.util.Collections; import java.util.List; /** @@ -65,17 +68,19 @@ public class ProfitSharingResult extends BaseWxPayResult implements Serializable /** * 获取分账接收方列表方法 * - * @return */ public List getReceiverList() { - if (receiverList == null && receivers != null && receivers.length() > 0) { - List tempList = GSON.fromJson(receivers, List.class); - for (String str : tempList) { - Receiver receiver = GSON.fromJson(str, Receiver.class); - receiverList.add(receiver); - } + if (receiverList != null) { + return receiverList; } - return receiverList; + + if (StringUtils.isNotEmpty(receivers)) { + this.receiverList = GSON.fromJson(receivers, new TypeToken>() { + }.getType()); + return receiverList; + } + + return Collections.emptyList(); } @Override diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResultTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResultTest.java new file mode 100644 index 000000000..f9bf0e838 --- /dev/null +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/profitsharing/ProfitSharingResultTest.java @@ -0,0 +1,71 @@ +package com.github.binarywang.wxpay.bean.profitsharing; + +import org.testng.annotations.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ProfitSharingResultTest { + + @Test + public void testGetReceiverList() { + ProfitSharingResult profitSharingResult = ProfitSharingResult.fromXML("\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "\t\n" + + "\t\t\n" + + "\t\n" + + "", ProfitSharingResult.class); + + List receiverList = profitSharingResult.getReceiverList(); + assertThat(receiverList).isNotEmpty(); + } +}