[英]WSO2 EI, Iterate through JSON Array
我正在通過WSO2 EI創建簡單的REST API。 我正在發送此json:
{
"array": ["Hello", "this", "is", "an", "arrray", "1", 3, 4]
}
我想遍歷數組中的每個元素,並在日志中顯示它們。
我為此創建了這樣的序列:
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="for_each_test_seq_in" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="json-eval($.array)" name="array"
scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<log level="full">
<property expression="get-property('array')" name="ARRAY" xmlns:ns="http://org.apache.synapse/xsd"/>
</log>
<iterate expression="get-property('array')" id="iterate_one" xmlns:ns="http://org.apache.synapse/xsd">
<target sequence="anon">
<sequence>
<log level="full"/>
</sequence>
</target>
</iterate>
<respond/>
</sequence>
啟動它時,出現以下錯誤消息:
[2017-09-19 16:06:40,236] [] ERROR - SynapseXPath Evaluation of the XPath expression get-property('array') resulted in an error
org.jaxen.UnresolvableException: No Such Function get-property
at
org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:127)
at org.jaxen.ContextSupport.getFunction(ContextSupport.java:242)
at org.jaxen.Context.getFunction(Context.java:216)
...
我不明白為什么get-property
未知。 我知道get-property
是XPath函數。
我只想遍歷array
元素的每個元素並對其進行處理。
可能嗎?
這是實現。
其余服務的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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.