【问题标题】:Dynamically add new element to json payload in WSO2 EI by using enrich mediator使用丰富的调解器动态地将新元素添加到 WSO2 EI 中的 json 有效负载
【发布时间】:2019-11-22 05:24:42
【问题描述】:

我需要用动态传递给现有有效负载而不是静态值的新元素来丰富现有的 json 有效负载。谁能帮帮我?

现有负载:

{
  "Type":"CAR",
  "Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
  "ListingType":"New",
  "SaleStatus":"For Sale"
}

预期:

{
  "Type":"CAR",
  "Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
  "ListingType":"New",
  "SaleStatus":"For Sale",
  "messageId":"urn:uuid:ccdafb72-c4"
}

这里的messageId是ESB自动生成的MessageID。

 <!-- ************API Request set to incomingRequest property************ -->
 <property description="incomingRequest" expression="json-eval($.)" name="incomingRequest" 
 scope="default" type="STRING"/>
<payloadFactory media-type="json">
    <format>$1</format>
    <args>
        <arg evaluator="xml" expression="get-property('incomingRequest')"/>
    </args>
</payloadFactory>
<enrich description="">
    <source type="inline" clone="true">
        <messageId xmlns="">evaluate(get-property('operation','messageId')) 
  </messageId>
    </source>
    <target action="child" xpath="//jsonObject" />
</enrich>
<enrich>
    <source clone="true" xpath="//jsonObject" />
    <target type="body" />
</enrich>
 <log level="full"/>

**Getting Wrong output** 

{
  "Type":"CAR",
  "Identifier":"2db23c39-9d3f-4e61-b3c5-e8725a2f1b90",
  "ListingType":"New",
  "SaleStatus":"For Sale",
  "messageId": "evaluate(get-property('operation','messageId'))"
}

注意: 我关注了this

【问题讨论】:

    标签: json wso2ei


    【解决方案1】:

    不评估丰富中介上的内联内容。 (视为字符串) 因此,我们可以使用属性中介器来评估表达式。

    <property name="prop1" expression="get-property('operation','messageId')"/>
    

    那么我们就可以在丰富的中介器中使用上面的属性了。

    <enrich description="">
        <source type="property" property="prop1"></source>
        <target action="child" xpath="//jsonObject" />
    </enrich>
    

    顺便说一句,在最新的产品版本中,Enrich 调解器具有原生 JSON 支持。 更多详情Here

    【讨论】:

    • 嗨@Lahiru,感谢您的回复。我已经尝试过您提供的相同方式。 MessageId 不会添加到现有的有效负载中。它显示响应就像我收到的任何请求一样。 $1
    【解决方案2】:

    我有类似的情况,我想将一些元素附加到收到的 json 响应中:

    • 首先我收到了一个属性的响应: 属性表达式="json-eval($)" 名称="响应" 范围="默认" 类型="字符串"

    • 然后我创建了一个有效负载并添加了新属性:

     <payloadFactory media-type="json">
                               <format>
                                    {"details": $1,
                                    "timestamp":"$2",
                                    "value":"$3"}
                                </format>
                                <args>
                                    <arg evaluator="xml"
                                        expression="get-property('RESPONSE')" />
                                    <arg evaluator="xml"
                                        expression="get-property('REQUEST_TIMESTAMP')" />
                                    <arg evaluator="xml"
                                        expression="get-property('VALUE')" />
                                </args>
                            </payloadFactory>
    

    然后我使用环回返回响应。请注意,REQUEST_TIMESTAMP 和 VALUE 被定义为属性。

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2013-01-07
      • 2020-02-24
      相关资源
      最近更新 更多