10000 #629 修复WxPayOrderNotifyResult解析xml报错问题 · binarywang/WxJava@a10007a · GitHub
[go: up one dir, main page]

Skip to content

Commit a10007a

Browse files
committed
#629 修复WxPayOrderNotifyResult解析xml报错问题
1 parent ba7905a commit a10007a

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/converter/WxPayOrderNotifyResultConverter.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyCoupon;
44
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
55
import com.google.common.base.Function;
6+
import com.google.common.collect.Lists;
67
import com.google.common.collect.Maps;
78
import com.thoughtworks.xstream.annotations.XStreamAlias;
89
import com.thoughtworks.xstream.converters.MarshallingContext;
@@ -21,6 +22,9 @@
2122
import java.util.List;
2223
import java.util.Map;
2324

25+
/**
26+
* @author aimilin
27+
*/
2428
public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter {
2529

2630
public WxPayOrderNotifyResultConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
@@ -72,26 +76,26 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
7276
fields.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields()));
7377
Map<String, Field> fieldMap = getFieldMap(fields);
7478

75-
List<WxPayOrderNotifyCoupon> coupons = new ArrayList<>(10);
79+
Map<Integer, WxPayOrderNotifyCoupon> coupons = Maps.newTreeMap();
7680
while (reader.hasMoreChildren()) {
7781
reader.moveDown();
7882
if (fieldMap.containsKey(reader.getNodeName())) {
7983
Field field = fieldMap.get(reader.getNodeName());
80-
setFieldValue(context, obj, field);
84+
this.setFieldValue(context, obj, field);
8185
} else if (StringUtils.startsWith(reader.getNodeName(), "coupon_id_")) {
8286
String id = (String) context.convertAnother(obj, String.class);
83-
getIndex(coupons, reader.getNodeName()).setCouponId(id);
87+
this.getElement(coupons, reader.getNodeName()).setCouponId(id);
8488
} else if (StringUtils.startsWith(reader.getNodeName(), "coupon_type_")) {
8589
String type = (String) context.convertAnother(obj, String.class);
86-
getIndex(coupons, reader.getNodeName()).setCouponType(type);
90+
this.getElement(coupons, reader.getNodeName()).setCouponType(type);
8791
} else if (StringUtils.startsWith(reader.getNodeName(), "coupon_fee_")) {
8892
Integer fee = (Integer) context.convertAnother(obj, Integer.class);
89-
getIndex(coupons, reader.getNodeName()).setCouponFee(fee);
93+
this.getElement(coupons, reader.getNodeName()).setCouponFee(fee);
9094
}
9195
reader.moveUp();
9296
}
9397

94-
obj.setCouponList(coupons);
98+
obj.setCouponList(Lists.newArrayList(coupons.values()));
9599
return obj;
96100
}
97101

@@ -102,12 +106,12 @@ private void setFieldValue(UnmarshallingContext context, WxPayOrderNotifyResult
102106
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass());
103107
pd.getWriteMethod().invoke(obj, val);
104108
}
105-
} catch (Exception e) {
109+
} catch (Exception ignored) {
106110
}
107111
}
108112

109113
private Map<String, Field> getFieldMap(List<Field> fields) {
110-
Map<String, Field> fieldMap = Maps.uniqueIndex(fields, new Function<Field, String>() {
114+
return Maps.uniqueIndex(fields, new Function<Field, String>() {
111115
@Override
112116
public String apply(Field field) {
113117
if (field.isAnnotationPresent(XStreamAlias.class)) {
@@ -116,14 +120,14 @@ public String apply(Field field) {
116120
return field.getName();
117121
}
118122
});
119-
return fieldMap;
120123
}
121124

122-
private WxPayOrderNotifyCoupon getIndex(List<WxPayOrderNotifyCoupon> coupons, String nodeName) {
123-
Integer index = Integer.valueOf(StringUtils.substring(nodeName, nodeName.lastIndexOf("_") + 1));
124-
if (index >= coupons.size() || coupons.get(index) == null) {
125-
coupons.add(index, new WxPayOrderNotifyCoupon());
125+
private WxPayOrderNotifyCoupon getElement(Map<Integer, WxPayOrderNotifyCoupon> coupons, String nodeName) {
126+
Integer index = Integer.valueOf(StringUtils.substringAfterLast(nodeName, "_"));
127+
if (coupons.get(index) == null) {
128+
coupons.put(index, new WxPayOrderNotifyCoupon());
126129
}
130+
127131
return coupons.get(index);
128132
}
129133
}

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/notify/WxPayOrderNotifyResultTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class WxPayOrderNotifyResultTest {
1414
@Test
15-
public void testFromXML() throws Exception {
15+
public void testFromXML() {
1616
String xmlString = "<xml>\n" +
1717
" <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" +
1818
" <attach><![CDATA[支付测试]]></attach>\n" +
@@ -32,12 +32,12 @@ public void testFromXML() throws Exception {
3232
" <trade_type><![CDATA[JSAPI]]></trade_type>\n" +
3333
" <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>\n" +
3434
" <coupon_count>2</coupon_count>\n" +
35-
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
36-
" <coupon_id_0>10000</coupon_id_0>\n" +
37-
" <coupon_fee_0>100</coupon_fee_0>\n" +
3835
" <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" +
3936
" <coupon_id_1>10001</coupon_id_1>\n" +
4037
" <coupon_fee_1>200</coupon_fee_1>\n" +
38+
" <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" +
39+
" <coupon_id_0>10000</coupon_id_0>\n" +
40+
" <coupon_fee_0>100</coupon_fee_0>\n" +
4141
"</xml>";
4242

4343
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlString);

0 commit comments

Comments
 (0)
0