【问题标题】:Wso2 esb: Edit the response of one rest API and send that response to another rest apiWso2 esb:编辑一个 rest API 的响应并将该响应发送到另一个 rest api
【发布时间】:2014-11-26 11:07:53
【问题描述】:

我们有 2 个 REST API,必须编辑一个 REST API 响应并将其作为请求消息发送到另一个 REST API。

例如:第一个 api 响应是

<?xml version="1.0"?>
<api>
  <attribute_set_id>4</attribute_set_id>
  <type_id>simple</type_id>
  <sku>test_dress</sku>
</api>

现在我们必须将该响应编辑为

<?xml version="1.0"?>
<api>
  <id>4</id>
  <product_id>simple</product_id>
  <sku>test_dress</sku>
</api>

并将该响应作为请求消息发送到另一个 rest api。 是否可以通过 WSO2 ESB 提供任何示例以适用于上述场景。

如果我需要提供更多详细信息,请告诉我。

谢谢, 吉萨·古普塔

【问题讨论】:

    标签: wso2 wso2esb


    【解决方案1】:

    您必须自定义以满足您的需求的小样本:

    <?xml version="1.0" encoding="UTF-8"?>
    <proxy xmlns="http://ws.apache.org/ns/synapse"
           name="MyProxy"
           transports="http"
           startOnLoad="true"
           trace="disable">
        <description/>
        <target>
            <inSequence>
                <!-- send current message to rest api 1 (sample with a dynamic value inside rest url) -->
                <property name="uri.var.MyPath" value="mypath"/>
                <call>
                    <endpoint>
                        <http method="POST" uri-template="http://myhost/{uri.var.MyPath}/restapi1"/>
                    </endpoint>
                </call>
                <!-- here come the response from rest api 1 : transform this response -->
                <payloadFactory media-type="xml">
                    <format>
                        <api>
                            <id>$1</id>
                            <product_id>$2</product_id>
                            <sku>$3</sku>
                        </api>
                    </format>
                    <args>
                        <arg evaluator="xml" expression="//api/attribute_set_id/text()"/>
                        <arg evaluator="xml" expression="//api/type_id/text()"/>
                        <arg evaluator="xml" expression="//api/sku/text()"/>
                    </args>
                </payloadFactory>
                <!-- call rest api 2 -->
                <call>
                    <endpoint>
                        <http method="POST" uri-template="http://myhost/{uri.var.MyPath}/restapi2"/>
                    </endpoint>
                </call>
                <!-- send back response from restapi2 -->
                <send/>
            </inSequence>
            <outSequence/>
        </target>
    </proxy>
    

    【讨论】:

    • 如果第一个 API 响应有多个产品详细信息,我如何获取每个产品的参数并发送到第二个 API 请求。 4simpletest_dress5simple test_material
    【解决方案2】:

    【讨论】:

    • 我想在发送到第二个休息 api 之前编辑响应作为 到 和 到
    • 对以上内容感到抱歉。您应该为此使用 Payload Factory Mediator。
    【解决方案3】:

    您可以通过以下步骤进行操作

    • 使用&lt;proprty&gt;读取响应并将响应参数分配给某些变量
    • 使用&lt;payloadFactory&gt; 使用这些参数设置动态负载
    • 之后您可以使用&lt;send&gt; mediator 调用另一个休息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多