WSO2 EI,遍历JSON数组

D先生

我正在通过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函数。

我只想遍历element的每个元素并对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>

下方图片上的发帖请求示例 SoapUI请求响应

服务器状态日志

[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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章