我在wso2 esb中定义了一个api,它通过接收列表调用了两个内部API,并且传递了json响应,如下所示。(示例响应)
{
"name": "api1",
"response": "success",
"status": "1"
}
和
{
"name": "api2",
"response": "unsuccess",
"status": "2"
}
我需要通过将这两个响应聚合为单个响应来传递响应。我关于payloadfactory并且能够构建聚合响应。但我需要汇总来自这两个api的响应,并将响应生成为一个单独的json对象,然后将这两个响应包括在内,如下所示
{
"response1": {
"name": "api1",
"response": "success",
"status": "1"
},
"response2": {
"name": "api2",
"response": "unsuccess",
"status": "2"
}
}
那么如何用WSO2ESB来完成。我正在使用最新版本的ESB。
答案 0 :(得分:1)
我创建了三个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 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>
答案 1 :(得分:0)
嗯,这是丰富的调解员变得方便的地方。请试一试。我没有测试过这个,因为我现在没有做WSO2相关的事情。但我们热烈欢迎您的反馈。伪代码是这样的。
<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>