这里是实现。
休息服务的 WSO API。接收请求,记录消息正文,按顺序处理并发送回客户端。序列for_each_test_seq_in不会改变消息的内容
<?xml version="1.0" encoding="UTF-8"?>
<api context="/array" name="array" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<log>
<property expression="json-eval($.*)" name="==============="/>
</log>
<sequence key="for_each_test_seq_in"/>
<loopback/>
</inSequence>
<outSequence>
<send/>
</outSequence>
<faultSequence/>
</resource>
</api>
这是序列码
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="for_each_test_seq_in" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="json-eval($.array)" name="array" scope="default" type="STRING"/>
<log level="full">
<property expression="get-property('array')" name="ARRAY" xmlns:ns="http://org.apache.synapse/xsd"/>
</log>
<foreach expression="//array">
<sequence>
<log level="full"/>
</sequence>
</foreach>
</sequence>
下图发帖请求示例
服务器状态日志
[2017-10-16 23:55:18,375] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, =============== = [[34,234,"example",56.3,"mom"]]
[2017-10-16 23:55:18,379] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, ARRAY = [34,234,"example",56.3,"mom"], Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><array>34</array><array>234</array><array>example</array><array>56.3</array><array>mom</array></jsonObject></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,380] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>34</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,381] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>234</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,381] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>example</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,381] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>56.3</array></soapenv:Body></soapenv:Envelope>
[2017-10-16 23:55:18,382] [EI-Core] INFO - LogMediator To: /array, MessageID: urn:uuid:648f7e51-4c71-48d7-8d09-f914ce29a867, Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><array>mom</array></soapenv:Body></soapenv:Envelope>
第一行表示为
<log><property expression="json-eval($.*)" name="==============="/></log>
...=============== = [[34,234,"example",56.3,"mom"]]
第二个日志行是部分代码的结果
<log level="full">
<property expression="get-property('array')" name="ARRAY"/>
</log>
....Direction: request, ARRAY = [34,234,"example",56.3,"mom"], Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><jsonObject><array>34</array><array>234</array><array>example</array><array>56.3</array><array>mom</array></jsonObject></soapenv:Body></soapenv:Envelope>
如您所见,它有两种表示形式。一种是json格式:
ARRAY = [34,234,"example",56.3,"mom"]
其次是根据https://docs.wso2.com/display/ESB500/JSON+Support中描述的规则以 xml 表示法( JSON 有效负载的 XML 表示一章)
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<jsonObject>
<array>34</array>
<array>234</array>
<array>example</array>
<array>56.3</array>
<array>mom</array>
</jsonObject>
</soapenv:Body>
</soapenv:Envelope>
文档指出,如果您考虑 xml 中的 json 表示,则可以在 json 有效负载上应用 xpath。使用它只需申请每个使用 xpath //array获取数组的每个部分
结果原始数组,以 xml 表示,被数组的每个元素吐出并登录到控制台。您可以在其余日志中看到它
LogMediator To: /array, MessageID: urn:uuid:6... Envelope: <soapenv:Body><array>34</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6.... Envelope: <soapenv:Body><array>234</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6... Envelope: <soapenv:Body><array>example</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6.... Envelope: <soapenv:Body><array>56.3</array></soapenv:Body>
LogMediator To: /array, MessageID: urn:uuid:6.... Envelope: <soapenv:Body><array>mom</array></soapenv:Body>