簡體   English   中英

通過使用豐富的調解器動態添加新元素到 WSO2 EI 中的 json 有效負載

[英]Dynamically add new element to json payload in WSO2 EI by using enrich mediator

我需要使用動態傳遞給現有有效負載而不是 static 值的新元素來豐富現有的 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'))"
}

注意:我遵循了這個

不評估豐富介體上的內聯內容。 (視為字符串)因此,我們可以使用屬性中介器來評估表達式。

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

然后我們可以在豐富中介者中使用上述屬性。

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

順便說一句,在最新的產品版本中,Enrich 調解器具有原生 JSON 支持。 更多細節在這里

我有類似的情況,我想 append 一些元素到收到的 json 響應:

  • 首先我收到了一個屬性的響應: property expression="json-eval($)" name="RESPONSE" scope="default" type="STRING"

  • 然后我創建了一個有效負載並添加了新屬性:

 <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 被定義為屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM