在 wso2 esb 中使用聚合中介聚合多个 json 响应 [英] Aggregate multiple json responses with aggregate mediator in wso2 esb

查看:17
本文介绍了在 wso2 esb 中使用聚合中介聚合多个 json 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 wso2 esb 中定义了一个 api,它通过接收者列表调用两个内部 API,它们传递 json 响应如下.(示例响应)

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)

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

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

我需要通过将这两个响应聚合为一个响应来传递响应.我对 payloadfactory 很感兴趣,并且能够构建聚合响应.但是我需要聚合来自这 2 个 api 的任何响应,并将响应生成为一个 json 对象,并通过包括这两个响应来传递,如下所示

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

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

那么如何使用 WSO2ESB 完成.我正在使用最新版本的 ESB.

so how can a accomplish with WSO2ESB. I'm using latest version of ESB.

推荐答案

我创建了三个 API 并使用 Clone 聚合了 API 响应,下面是我的 API,用于聚合两个 API 端点的响应

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

这篇关于在 wso2 esb 中使用聚合中介聚合多个 json 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆