mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 10:38:42 +08:00 
			
		
		
		
	#629 修复WxPayOrderNotifyResult解析xml报错问题
This commit is contained in:
		| @ -3,6 +3,7 @@ package com.github.binarywang.wxpay.converter; | |||||||
| import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyCoupon; | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyCoupon; | ||||||
| import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; | import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult; | ||||||
| import com.google.common.base.Function; | import com.google.common.base.Function; | ||||||
|  | import com.google.common.collect.Lists; | ||||||
| import com.google.common.collect.Maps; | import com.google.common.collect.Maps; | ||||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||||
| import com.thoughtworks.xstream.converters.MarshallingContext; | import com.thoughtworks.xstream.converters.MarshallingContext; | ||||||
| @ -21,6 +22,9 @@ import java.util.Arrays; | |||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
|  |  | ||||||
|  | /** | ||||||
|  |  * @author aimilin | ||||||
|  |  */ | ||||||
| public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter { | public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter { | ||||||
|  |  | ||||||
|   public WxPayOrderNotifyResultConverter(Mapper mapper, ReflectionProvider reflectionProvider) { |   public WxPayOrderNotifyResultConverter(Mapper mapper, ReflectionProvider reflectionProvider) { | ||||||
| @ -72,26 +76,26 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter | |||||||
|     fields.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields())); |     fields.addAll(Arrays.asList(obj.getClass().getSuperclass().getDeclaredFields())); | ||||||
|     Map<String, Field> fieldMap = getFieldMap(fields); |     Map<String, Field> fieldMap = getFieldMap(fields); | ||||||
|  |  | ||||||
|     List<WxPayOrderNotifyCoupon> coupons = new ArrayList<>(10); |     Map<Integer, WxPayOrderNotifyCoupon> coupons = Maps.newTreeMap(); | ||||||
|     while (reader.hasMoreChildren()) { |     while (reader.hasMoreChildren()) { | ||||||
|       reader.moveDown(); |       reader.moveDown(); | ||||||
|       if (fieldMap.containsKey(reader.getNodeName())) { |       if (fieldMap.containsKey(reader.getNodeName())) { | ||||||
|         Field field = fieldMap.get(reader.getNodeName()); |         Field field = fieldMap.get(reader.getNodeName()); | ||||||
|         setFieldValue(context, obj, field); |         this.setFieldValue(context, obj, field); | ||||||
|       } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_id_")) { |       } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_id_")) { | ||||||
|         String id = (String) context.convertAnother(obj, String.class); |         String id = (String) context.convertAnother(obj, String.class); | ||||||
|         getIndex(coupons, reader.getNodeName()).setCouponId(id); |         this.getElement(coupons, reader.getNodeName()).setCouponId(id); | ||||||
|       } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_type_")) { |       } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_type_")) { | ||||||
|         String type = (String) context.convertAnother(obj, String.class); |         String type = (String) context.convertAnother(obj, String.class); | ||||||
|         getIndex(coupons, reader.getNodeName()).setCouponType(type); |         this.getElement(coupons, reader.getNodeName()).setCouponType(type); | ||||||
|       } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_fee_")) { |       } else if (StringUtils.startsWith(reader.getNodeName(), "coupon_fee_")) { | ||||||
|         Integer fee = (Integer) context.convertAnother(obj, Integer.class); |         Integer fee = (Integer) context.convertAnother(obj, Integer.class); | ||||||
|         getIndex(coupons, reader.getNodeName()).setCouponFee(fee); |         this.getElement(coupons, reader.getNodeName()).setCouponFee(fee); | ||||||
|       } |       } | ||||||
|       reader.moveUp(); |       reader.moveUp(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     obj.setCouponList(coupons); |     obj.setCouponList(Lists.newArrayList(coupons.values())); | ||||||
|     return obj; |     return obj; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @ -102,12 +106,12 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter | |||||||
|         PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass()); |         PropertyDescriptor pd = new PropertyDescriptor(field.getName(), obj.getClass()); | ||||||
|         pd.getWriteMethod().invoke(obj, val); |         pd.getWriteMethod().invoke(obj, val); | ||||||
|       } |       } | ||||||
|     } catch (Exception e) { |     } catch (Exception ignored) { | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private Map<String, Field> getFieldMap(List<Field> fields) { |   private Map<String, Field> getFieldMap(List<Field> fields) { | ||||||
|     Map<String, Field> fieldMap = Maps.uniqueIndex(fields, new Function<Field, String>() { |     return Maps.uniqueIndex(fields, new Function<Field, String>() { | ||||||
|       @Override |       @Override | ||||||
|       public String apply(Field field) { |       public String apply(Field field) { | ||||||
|         if (field.isAnnotationPresent(XStreamAlias.class)) { |         if (field.isAnnotationPresent(XStreamAlias.class)) { | ||||||
| @ -116,14 +120,14 @@ public class WxPayOrderNotifyResultConverter extends AbstractReflectionConverter | |||||||
|         return field.getName(); |         return field.getName(); | ||||||
|       } |       } | ||||||
|     }); |     }); | ||||||
|     return fieldMap; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private WxPayOrderNotifyCoupon getIndex(List<WxPayOrderNotifyCoupon> coupons, String nodeName) { |   private WxPayOrderNotifyCoupon getElement(Map<Integer, WxPayOrderNotifyCoupon> coupons, String nodeName) { | ||||||
|     Integer index = Integer.valueOf(StringUtils.substring(nodeName, nodeName.lastIndexOf("_") + 1)); |     Integer index = Integer.valueOf(StringUtils.substringAfterLast(nodeName, "_")); | ||||||
|     if (index >= coupons.size() || coupons.get(index) == null) { |     if (coupons.get(index) == null) { | ||||||
|       coupons.add(index, new WxPayOrderNotifyCoupon()); |       coupons.put(index, new WxPayOrderNotifyCoupon()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return coupons.get(index); |     return coupons.get(index); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ import org.testng.annotations.*; | |||||||
|  */ |  */ | ||||||
| public class WxPayOrderNotifyResultTest { | public class WxPayOrderNotifyResultTest { | ||||||
|   @Test |   @Test | ||||||
|   public void testFromXML() throws Exception { |   public void testFromXML() { | ||||||
|     String xmlString = "<xml>\n" + |     String xmlString = "<xml>\n" + | ||||||
|       "  <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" + |       "  <appid><![CDATA[wx2421b1c4370ec43b]]></appid>\n" + | ||||||
|       "  <attach><![CDATA[支付测试]]></attach>\n" + |       "  <attach><![CDATA[支付测试]]></attach>\n" + | ||||||
| @ -32,12 +32,12 @@ public class WxPayOrderNotifyResultTest { | |||||||
|       "  <trade_type><![CDATA[JSAPI]]></trade_type>\n" + |       "  <trade_type><![CDATA[JSAPI]]></trade_type>\n" + | ||||||
|       "  <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>\n" + |       "  <transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>\n" + | ||||||
|       "   <coupon_count>2</coupon_count>\n" + |       "   <coupon_count>2</coupon_count>\n" + | ||||||
|       "   <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" + |  | ||||||
|       "   <coupon_id_0>10000</coupon_id_0>\n" + |  | ||||||
|       "   <coupon_fee_0>100</coupon_fee_0>\n" + |  | ||||||
|       "   <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" + |       "   <coupon_type_1><![CDATA[NO_CASH]]></coupon_type_1>\n" + | ||||||
|       "   <coupon_id_1>10001</coupon_id_1>\n" + |       "   <coupon_id_1>10001</coupon_id_1>\n" + | ||||||
|       "   <coupon_fee_1>200</coupon_fee_1>\n" + |       "   <coupon_fee_1>200</coupon_fee_1>\n" + | ||||||
|  |       "   <coupon_type_0><![CDATA[CASH]]></coupon_type_0>\n" + | ||||||
|  |       "   <coupon_id_0>10000</coupon_id_0>\n" + | ||||||
|  |       "   <coupon_fee_0>100</coupon_fee_0>\n" + | ||||||
|       "</xml>"; |       "</xml>"; | ||||||
|  |  | ||||||
|     WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlString); |     WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlString); | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang