添加存储在 Iterator 内部的属性作为 Iterator 外部的兄弟姐妹

Adding properties stored inside Iterator as siblings outside Iterator

我有一个要求,我要在 Iterator 中准备一些有效载荷并将其存储到 属性 中。 现在我想读取存储在 属性 中的有效载荷,并通过将 属性 值添加为兄弟来构建一些响应。

<iterate expression="//result" id="Results">
  <property name="test1" expression="//test1" />
  <property name="test2" expression="//test2" />
  <payloadFactory media-type="json">
     <format>[{     "test1":"",
                      "test2":""
       }]</format>
     <args>
        <arg evaluator="xml" expression="get-property('test1')" />
        <arg evaluator="xml" expression="get-property('test2')" />
     </args>
  </payloadFactory>
  <property name="ITERATOR_DATA_PAYLOAD" expression="$body" scope="operation" type="OM" />
</iterate>```

Enrich here all the ITERATOR_DATA_PAYLOAD property values here something like below by adding as siblings

```[{ "test1":"","test2":""},{"test1":"","test2":""},{ "test1":"","test2":""},{"test1":"","test2":""}]```

要将每次迭代的响应用于单个主体,您还必须使用聚合中介,前提是您正在调用端点。

要聚合响应,您必须在 API/Service 的 outSequence 中执行此操作,您必须在 Iterate 中提供环回,最后。

样本的 PFB API。

<api xmlns="http://ws.apache.org/ns/synapse" name="Iterate" context="/iterate">
   <resource methods="POST">
      <inSequence>
         <log>
            <property name=":: PAYLOAD ::" expression="$body"/>
         </log>
         <iterate continueParent="true" id="Results" preservePayload="true" expression="//result" sequential="true">
            <target>
               <sequence>
                  <property name="test1" expression="//test1"/>
                  <property name="test2" expression="//test2"/>
                  <payloadFactory media-type="json">
                     <format>{"test1":"","test2":""}</format>
                     <args>
                        <arg evaluator="xml" expression="get-property('test1')"/>
                        <arg evaluator="xml" expression="get-property('test2')"/>
                     </args>
                  </payloadFactory>
                  <log>
                     <property name=":: Iterated PAYLOAD ::" expression="$body"/>
                  </log>
                  <property name="ITERATOR_DATA_PAYLOAD" expression="$body" scope="operation" type="OM"/>
                  <call>
                     <endpoint>
                        <address uri="http://localhost:8280/echoAPI"/>
                     </endpoint>
                  </call>
                  <log>
                     <property name=":: Echo Response ::" expression="$body"/>
                  </log>
                  <loopback/>
               </sequence>
            </target>
         </iterate>
      </inSequence>
      <outSequence>
         <property name="response" scope="default">
                <Responses/>
            </property>
            <aggregate id="Results">
                <completeCondition>
                    <messageCount max="-1" min="-1"/>
                </completeCondition>
                <onComplete enclosingElementProperty="response" expression="$body/*[1]">
                    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
                    <property name="ContentType" scope="axis2" type="STRING" value="application/json"/>
                    <respond/>
                </onComplete>
            </aggregate>
      </outSequence>
   </resource>
</api>

要求:

{
    "result": [
        {
            "test1": "test1",
            "test2": "test2"
        },
        {
            "test1": "test3",
            "test2": "test4"
        }
    ]
}

回复:

{
    "Responses": [
        {
            "test1": "test1",
            "test2": "test2"
        },
        {
            "test1": "test3",
            "test2": "test4"
        }
    ]
}

在这里,我使用了回显API,它将发送与响应相同的请求。对于您的用例,您可以使用实际端点。

这就是迭代和聚合在 WSO2 中协同工作的方式 EI/ESB。