简体   繁体   English

在wso2 esb中使用聚合介体聚合多个json响应

[英]Aggregate multiple json responses with aggregate mediator in wso2 esb

I have defined a api in wso2 esb and it calls two internal APIs through recepient list and which are passing json responses as follows.(sample responses) 我在wso2 esb中定义了一个api,它通过接收列表调用了两个内部API,并且传递了json响应,如下所示。(示例响应)

{
  "name": "api1",
  "response": "success",
  "status": "1"
}

and

{
  "name": "api2",
  "response": "unsuccess",
  "status": "2"
}

I need to pass the response by aggregating both of these responses as a single response. 我需要通过将这两个响应聚合为单个响应来传递响应。 I red about payloadfactory and able to construct aggregated response. 我关于payloadfactory并且能够构建聚合响应。 But i need to aggregate whatever the responses coming from these 2 apis and generate response as one single json object and pass by including both of these responses as follows 但是我需要聚合来自这两个api的响应,并将响应作为一个单独的json对象生成并通过包括这两个响应,如下所示

 {
    "response1": {
        "name": "api1",
        "response": "success",
        "status": "1"
    },
    "response2": {
        "name": "api2",
        "response": "unsuccess",
        "status": "2"
    }
}

so how can a accomplish with WSO2ESB. 那么如何用WSO2ESB来完成。 I'm using latest version of ESB. 我正在使用最新版本的ESB。

I have created three APIs and aggregated the API responses using Clone ,Below is my API which is used for aggregating responses of two API endpoints 我创建了三个API并使用Clone聚合API响应,下面是我的API,用于聚合两个API端点的响应

<api xmlns="http://ws.apache.org/ns/synapse" name="aggregateResponse" context="/aggregate">
   <resource methods="POST">
      <inSequence>
         <clone id="aggr">
            <target>
               <sequence>
                  <call>
                     <endpoint>
                        <http method="GET" uri-template="http://localhost:8280/getresponse1"/>
                     </endpoint>
                  </call>
                  <log>
                     <property name="Logger1" expression="json-eval($.)"/>
                  </log>
               </sequence>
            </target>
            <target>
               <sequence>
                  <call>
                     <endpoint>
                        <http method="GET" uri-template="http://localhost:8280/getResponse2"/>
                     </endpoint>
                  </call>
                  <log>
                     <property name="Logger1" expression="json-eval($.)"/>
                  </log>
               </sequence>
            </target>
         </clone>
         <payloadFactory media-type="json">
            <format>{"responses":{ "name":"$1","response":"$2","status":"$3"}}</format>
            <args>
               <arg evaluator="json" expression="$.name"/>
               <arg evaluator="json" expression="$.response"/>
               <arg evaluator="json" expression="$.status"/>
            </args>
         </payloadFactory>
         <loopback/>
      </inSequence>
      <outSequence>
         <property name="res" scope="default">
            <ResponseDetail xmlns=""/>
         </property>
         <aggregate id="aggr">
            <completeCondition>
               <messageCount min="-1" max="-1"/>
            </completeCondition>
            <onComplete expression="$body//responses" enclosingElementProperty="res">
               <payloadFactory media-type="json">
                  <format>{"response1":$1 ,"response2":$2}</format>
                  <args>
                     <arg evaluator="json" expression="$.ResponseDetail.responses[0]"/>
                     <arg evaluator="json" expression="$.ResponseDetail.responses[1]"/>
                  </args>
               </payloadFactory>
               <send/>
            </onComplete>
         </aggregate>
      </outSequence>
   </resource>
</api>

API 1: API 1:

<api xmlns="http://ws.apache.org/ns/synapse" name="response1" context="/getresponse1">
   <resource methods="GET">
      <inSequence>
         <payloadFactory media-type="json">
            <format>{  "name": "api1",  "response": "success",  "status": "1"}</format>
            <args/>
         </payloadFactory>
         <respond/>
      </inSequence>
   </resource>
</api>

API 2: API 2:

<api xmlns="http://ws.apache.org/ns/synapse" name="response2" context="/getResponse2">
   <resource methods="GET">
      <inSequence>
         <payloadFactory media-type="json">
            <format>{  "name": "api2",  "response": "unsuccess",  "status": "2"}</format>
            <args/>
         </payloadFactory>
         <respond/>
      </inSequence>
   </resource>
</api>

Well, this is where enrich mediator becomes handy. 那么,这就是丰富的调解员变得方便的地方。 Please try this out. 请试一试。 I have not tested this since I am not doing WSO2 related stuffs now. 我没有测试过这个,因为我现在没有做WSO2相关的事情。 But your feedback is warmly welcome. 但我们热烈欢迎您的反馈。 The pseudo code is something like this. 伪代码是这样的。

<call>
    <endpoint>
       <http method="GET" uri-template="http://www.mocky.io/v2/some-ep"/>
    </endpoint>
 </call>
 <enrich>
    <source type="body" clone="true"/>
    <target type="property" property="first-json"/>
 </enrich>
    <call>
    <endpoint>
       <http method="GET" uri-template="http://www.mocky.io/v2/another-ep"/>
    </endpoint>
 </call>
 <enrich>
    <source type="property" property="first-json" clone="true"/>
    <target action="sibling" xpath="//"/>
 </enrich>
 </respond>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM