繁体   English   中英

如何在 wso2 中组合 2 个 json 有效载荷?

[英]How to combine 2 json payloads in wso2?

我正在使用 XSLT 调解器来形成以下 JSON 有效负载:

{
    "products": [
        {
          
            "product_id": 1,
            "product_name" : "abc"
           
        },
        {
            
            "product_id": 2,
           "product_name" : "xyz"
            
        }
    ]
}

Based on the response I get from the api using the above payload, I need to create another json payload(below) and append it to make another api call.

{
"amount": {
    "total_amount": 0.46,
    "total_tax": 0
  }
 }

我希望最终的有效载荷看起来像

 {
    "products": [
        {
          
            "product_id": 1,
            "product_name" : "abc"
           
        },
        {
            
            "product_id": 2,
           "product_name" : "xyz"
            
        }
    ],
    
    "amount": {
    "total_amount": 0.46,
    "total_tax": 0
  }
}

如何在 WSO2 集成工作室上实现这一目标?

您可以使用 DataMapper 中介器提供输入和 output 架构,并将传入的有效负载转换为您需要的方式。 Provide either JSON or XML schema that you are receiving from the 1st service as the input schema to DataMapper and provide the schema of the expected result in either JSON or XML to the DataMatter as output schema. 之后,您可以 map 两个输入/ output 字段,在运行时,DataMapper 将为您进行消息翻译。

有关更多详细信息 - https://apim.docs.wso2.com/en/latest/tutorials/integration-tutorials/transforming-message-content/

谢谢!

我认为XSLT过分了,我不认为Datamapper在这里是一个可行的选择,因为我们正在处理多个输入。 为此,您可以简单地使用Enrich Mediator 这是您的有效负载的示例。 我试图提供一个接近您要求的示例,我已经对有效负载进行了硬编码,并且Payload Factory描述了您的后端调用响应和原始 PL。

<?xml version="1.0" encoding="UTF-8"?>
<api context="/json" name="TestAPI" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="GET">
        <inSequence>
            <payloadFactory description="Build JSON" media-type="json">
                <format>
                    {
                        "products": [
                            {
                                "product_id": 1,
                                "product_name" : "abc"   
                            },
                            {  
                               "product_id": 2,
                               "product_name" : "xyz"                               
                            }
                        ]
                    }
                    </format>
                <args/>
            </payloadFactory>
            <enrich description="First save the Original PL to a property" >
                <source clone="true" type="body"/>
                <target property="ORIGINAL_PL" type="property"/>
            </enrich>
            <payloadFactory description="Build JSON second PL" media-type="json">
                <format>
                    {
                        "amount": {
                            "total_amount": 0.46,
                            "total_tax": 0
                          }
                    }
                    </format>
                <args/>
            </payloadFactory>
            <enrich description="Save the PL from the second request to another property">
                <source clone="true" type="body"/>
                <target property="SECOND_PL" type="property"/>
            </enrich>
            <enrich description="Restore original payload">
                <source clone="false" property="ORIGINAL_PL" type="property"/>
                <target type="body"/>
            </enrich>
            <enrich description="Now add the second PL as a Child to the original" >
                <source clone="true" property="SECOND_PL" type="property"/>
                <target action="child" xpath="json-eval($)"/>
            </enrich>
            <log level="full"/>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

这是上面的结果。

{
  "products": [
    {
      "product_id": 1,
      "product_name": "abc"
    },
    {
      "product_id": 2,
      "product_name": "xyz"
    }
  ],
  "amount": {
    "total_amount": 0.46,
    "total_tax": 0
  }
}

暂无
暂无

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

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