mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-04 20:57:47 +08:00
🎨 【小程序】回调消息解析类WxMaMessage增加allFieldsMap属性,以存储所有xml消息报文
This commit is contained in:
parent
eb8942c871
commit
13c80294a2
@ -39,7 +39,18 @@ public class XmlUtils {
|
||||
Element root = doc.getRootElement();
|
||||
List<Element> elements = root.elements();
|
||||
for (Element element : elements) {
|
||||
map.put(element.getName(), element2MapOrString(element));
|
||||
String elementName = element.getName();
|
||||
if (map.containsKey(elementName)) {
|
||||
if (map.get(elementName) instanceof List) {
|
||||
((List<Object>) map.get(elementName)).add(element2MapOrString(element));
|
||||
} else {
|
||||
List<Object> value = Lists.newArrayList(map.get(elementName));
|
||||
value.add(element2MapOrString(element));
|
||||
map.put(elementName, value);
|
||||
}
|
||||
} else {
|
||||
map.put(elementName, element2MapOrString(element));
|
||||
}
|
||||
}
|
||||
} catch (DocumentException | SAXException e) {
|
||||
throw new WxRuntimeException(e);
|
||||
|
@ -9,6 +9,7 @@ import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamConverter;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.XmlUtils;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -17,6 +18,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
@ -26,6 +28,11 @@ import java.nio.charset.StandardCharsets;
|
||||
public class WxMaMessage implements Serializable {
|
||||
private static final long serialVersionUID = -3586245291677274914L;
|
||||
|
||||
/**
|
||||
* 使用dom4j解析的存放所有xml属性和值的map.
|
||||
*/
|
||||
private Map<String, Object> allFieldsMap;
|
||||
|
||||
@SerializedName("Encrypt")
|
||||
@XStreamAlias("Encrypt")
|
||||
@XStreamConverter(value = XStreamCDataConverter.class)
|
||||
@ -206,9 +213,12 @@ public class WxMaMessage implements Serializable {
|
||||
private WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson uselessMsg;
|
||||
|
||||
public static WxMaMessage fromXml(String xml) {
|
||||
return XStreamTransformer.fromXml(WxMaMessage.class, xml);
|
||||
WxMaMessage message = XStreamTransformer.fromXml(WxMaMessage.class, xml);
|
||||
message.setAllFieldsMap(XmlUtils.xml2Map(xml));
|
||||
return message;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static WxMaMessage fromXml(InputStream is) {
|
||||
return XStreamTransformer.fromXml(WxMaMessage.class, is);
|
||||
}
|
||||
|
@ -3,6 +3,10 @@ package cn.binarywang.wx.miniapp.bean;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
|
||||
@ -212,6 +216,7 @@ public class WxMaMessageTest {
|
||||
wxMessage = WxMaMessage.fromJson(json);
|
||||
checkSubscribeMsgSentEvent(wxMessage);
|
||||
}
|
||||
|
||||
private void checkSubscribeMsgSentEvent(WxMaMessage wxMessage) {
|
||||
assertEquals(wxMessage.getToUser(), "gh_123456789abc");
|
||||
assertEquals(wxMessage.getFromUser(), "o7esq5PHRGBQYmeNyfG064wEFVpQ");
|
||||
@ -226,4 +231,64 @@ public class WxMaMessageTest {
|
||||
assertEquals(event.getErrorStatus(), "success");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromXmlForAllFieldsMap() {
|
||||
String xml = "<xml>\n" +
|
||||
" <ToUserName><![CDATA[gh_3953b390c11d]]></ToUserName>\n" +
|
||||
" <FromUserName><![CDATA[ofYMP5JFT4SD7EX1LQv3IWrciBSo]]></FromUserName>\n" +
|
||||
" <CreateTime>1642658087</CreateTime>\n" +
|
||||
" <MsgType><![CDATA[event]]></MsgType>\n" +
|
||||
" <Event><![CDATA[add_express_path]]></Event>\n" +
|
||||
" <DeliveryID><![CDATA[TEST]]></DeliveryID>\n" +
|
||||
" <WayBillId><![CDATA[01234567894_waybill_id]]></WayBillId>\n" +
|
||||
" <Version>16</Version>\n" +
|
||||
" <Count>2</Count>\n" +
|
||||
" <Actions>\n" +
|
||||
" <ActionTime>1642605533</ActionTime>\n" +
|
||||
" <ActionType>300001</ActionType>\n" +
|
||||
" <ActionMsg><![CDATA[揽件阶段-揽件成功]]></ActionMsg>\n" +
|
||||
" <Lat>0</Lat>\n" +
|
||||
" <Lng>0</Lng>\n" +
|
||||
" </Actions>\n" +
|
||||
" <Actions>\n" +
|
||||
" <ActionTime>1642605533</ActionTime>\n" +
|
||||
" <ActionType>100001</ActionType>\n" +
|
||||
" <ActionMsg><![CDATA[揽件阶段-揽件成功]]></ActionMsg>\n" +
|
||||
" <Lat>0</Lat>\n" +
|
||||
" <Lng>0</Lng>\n" +
|
||||
" </Actions>\n" +
|
||||
" <OrderId><![CDATA[01234567894]]></OrderId>\n" +
|
||||
"</xml>";
|
||||
|
||||
WxMaMessage wxMessage = WxMaMessage.fromXml(xml);
|
||||
Map<String, Object> allFieldsMap = wxMessage.getAllFieldsMap();
|
||||
assertThat(allFieldsMap).isNotEmpty()
|
||||
.containsEntry("ToUserName", "gh_3953b390c11d")
|
||||
.containsEntry("FromUserName", "ofYMP5JFT4SD7EX1LQv3IWrciBSo")
|
||||
.containsEntry("CreateTime", "1642658087")
|
||||
.containsEntry("MsgType", "event")
|
||||
.containsEntry("Event", "add_express_path")
|
||||
.containsEntry("DeliveryID", "TEST")
|
||||
.containsEntry("WayBillId", "01234567894_waybill_id")
|
||||
.containsEntry("Version", "16")
|
||||
.containsEntry("Count", "2")
|
||||
.containsEntry("OrderId", "01234567894");
|
||||
|
||||
List<Map<String, Object>> actions = (List<Map<String, Object>>) allFieldsMap.get("Actions");
|
||||
assertThat(actions).isNotEmpty().hasSize(2);
|
||||
|
||||
assertThat(actions.get(0))
|
||||
.containsEntry("ActionTime", "1642605533")
|
||||
.containsEntry("ActionType", "300001")
|
||||
.containsEntry("ActionMsg", "揽件阶段-揽件成功")
|
||||
.containsEntry("Lat", "0")
|
||||
.containsEntry("Lng", "0");
|
||||
|
||||
assertThat(actions.get(1))
|
||||
.containsEntry("ActionTime", "1642605533")
|
||||
.containsEntry("ActionType", "100001")
|
||||
.containsEntry("ActionMsg", "揽件阶段-揽件成功")
|
||||
.containsEntry("Lat", "0")
|
||||
.containsEntry("Lng", "0");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user