使用WSO2聚合器组合多个JSON数组

时间:2017-01-26 21:42:57

标签: wso2 wso2esb

我尝试使用WSO2 ESB将两个不同的数据源合并为一个数据源。因为不同的源具有不同的数据格式,我的方法是为每个端点创建一个代理来处理授权和有效负载格式化,以便两者都返回一个JSON数组。基于研究,我的假设是我可以使用聚合器mediaor来组合结果。在完成了大量示例之后,我已成功合并了两个数组,但其中一个数组总是重复。任何人都可以看到我做错了什么或有没有任何其他建议有关更好的方法来实现两个JSON数组的组合?

我已设置以下代码示例来模拟2个Feed,然后与我的代码结合使用:

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="testb_url1" startOnLoad="true" trace="disable"
  transports="https http" xmlns="http://ws.apache.org/ns/synapse">
  <target>
    <inSequence>
          <payloadFactory media-type="json">
        <format>[
            {"id": "1",
            "type": "object",
            "name": "first"},
            {"id": "2",
            "type": "object",
            "name": "second"}
            ]
            </format>
        <args/>
      </payloadFactory>
      <log level="full"/>
      <loopback/>
    </inSequence>
    <outSequence>
      <send/>
    </outSequence>
    <faultSequence/>
  </target>
</proxy>

网址编号2

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="testb_url2" startOnLoad="true" trace="disable"
  transports="https http" xmlns="http://ws.apache.org/ns/synapse">
  <target>
    <inSequence>
          <payloadFactory media-type="json">
        <format>[
            {"id": "10",
            "type": "object",
            "name": "ten"},
            {"id": "11",
            "type": "object",
            "name": "eleven"}
            ]
            </format>
        <args/>
      </payloadFactory>
      <log level="full"/>
      <loopback/>
    </inSequence>
    <outSequence>
      <send/>
    </outSequence>
    <faultSequence/>
  </target>
</proxy>

要合并的代码:

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="testb_combine" startOnLoad="true" trace="disable"
  transports="https http" xmlns="http://ws.apache.org/ns/synapse">
  <target>
    <inSequence>
         <property name="enclosing_element" scope="default">
            <jsonArray xmlns=""/>
         </property>
         <send>
            <endpoint>
               <recipientlist>
                  <endpoint>
                     <address uri="http://localhost:8280/services/testb_url1/" trace="disable"/>
                  </endpoint>
                  <endpoint>
                     <address uri="http://localhost:8280/services/testb_url2/" trace="disable"/>
                  </endpoint>
               </recipientlist>
            </endpoint>
         </send>    
    </inSequence>
     <outSequence>
         <enrich>
            <source clone="true" xpath="$body/jsonArray/jsonElement"/>
            <target action="child" xpath="$ctx:enclosing_element"/>
         </enrich>
         <aggregate>
            <completeCondition>
               <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete expression="$body/jsonArray/jsonElement"
                        enclosingElementProperty="enclosing_element">
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
    <faultSequence/>
  </target>
</proxy>

返回结果显示来自URL 1的数据重复:

[
  {
    "id": 1,
    "type": "object",
    "name": "first"
  },
  {
    "id": 2,
    "type": "object",
    "name": "second"
  },
  {
    "id": 1,
    "type": "object",
    "name": "first"
  },
  {
    "id": 2,
    "type": "object",
    "name": "second"
  },
  {
    "id": 10,
    "type": "object",
    "name": "ten"
  },
  {
    "id": 11,
    "type": "object",
    "name": "eleven"
  }
]

1 个答案:

答案 0 :(得分:3)

删除outsequence中的rich mediator,它可以像你想要的那样工作: 代理服务:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="testb_combine"
       transports="https http"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="enclosing_element" scope="default">
            <jsonArray xmlns=""/>
         </property>
         <send>
            <endpoint>
               <recipientlist>
                  <endpoint>
                     <address uri="http://localhost:8283/services/testb_url1/"/>
                  </endpoint>
                  <endpoint>
                     <address uri="http://localhost:8283/services/testb_url2/"/>
                  </endpoint>
               </recipientlist>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <aggregate>
            <completeCondition>
               <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete expression="$body/jsonArray/jsonElement"
                        enclosingElementProperty="enclosing_element">
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
      <faultSequence/>
   </target>
</proxy>

响应:

[
    {
        "id": 1,
        "type": "object",
        "name": "first"
    },
    {
        "id": 2,
        "type": "object",
        "name": "second"
    },
    {
        "id": 10,
        "type": "object",
        "name": "ten"
    },
    {
        "id": 11,
        "type": "object",
        "name": "eleven"
    }
]