简体   繁体   English

如何使用丰富介体 WSO2 动态添加 json 元素?

[英]How to dynamically add a json element using enrich mediator WSO2?

I tried using我尝试使用

 <property name="messageType" scope="axis2" type="STRING" value="application/xml"/>
    <property expression="get-property('orderSourceSF')" name="orderSource" scope="default" type="STRING"/>
    <enrich description="">
        <source type="property"  property="orderSource"></source>
        <target action="child" xpath="$body//jsonObject"/>
    </enrich>
    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
    <payloadFactory media-type="json">
           <format>{request : $1}</format>
           <args>
               <arg evaluator="xml" expression="$body//jsonObject"/>
           </args>
       </payloadFactory>

Here I need to add a element called orderSouce to the existing payload.在这里,我需要向现有负载添加一个名为 orderSouce 的元素。 How can I insert that element.我怎样才能插入那个元素。 Existing payload is a json request.现有负载是一个 json 请求。 I am capturing the value of orderSource, but not getting how to insert the element orderSource and the value.我正在捕获 orderSource 的值,但没有获得如何插入元素 orderSource 和值。

I believe you can use a mediation such as follows to achieve your use case.我相信您可以使用如下中介来实现您的用例。

    <?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="enrichProxy"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <property expression="json-eval($)"
                   name="orderSource"
                   scope="default"
                   type="STRING"/>
         <log>
            <property expression="json-eval($)" name="orderSource"/>
         </log>
         <call>
            <endpoint>
               <address uri="http://run.mocky.io/v3/9cf4b844-57c1-4fa5-a101-881dc36385bd"/>
            </endpoint>
         </call>
         <log level="full"/>
         <property name="messageType"
                   scope="axis2"
                   type="STRING"
                   value="application/json"/>
         <enrich>
            <source clone="false" property="orderSource" type="property"/>
            <target action="child" xpath="json-eval($)"/>
         </enrich>
         <payloadFactory media-type="json">
            <format>{"request" : $1}</format>
            <args>
               <arg evaluator="json" expression="$"/>
            </args>
         </payloadFactory>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>
                            

Let me explain what I have done here.让我解释一下我在这里做了什么。 Since you are dealing with JSON payloads please use the JSON paths [1] instead of XPATH in your mediation.由于您正在处理 JSON 有效负载,请在您的中介中使用 JSON 路径 [1] 而不是 XPATH。 This will avoid unnecessary data conversion in the mediation.这将避免中介中不必要的数据转换。

We are invoking the above proxy service with the following payload.我们正在使用以下有效负载调用上述代理服务。

{"orderSource":"value"}

We are capturing this payload with JSON path and store the value in the property mediator orderSource.我们正在使用 JSON 路径捕获此有效负载,并将值存储在属性中介器 orderSource 中。 Then we are making an endpoint call and it will return the following JSON payload.然后我们进行端点调用,它将返回以下 JSON 有效负载。

{"first":"response"}

As per my understanding, you want to add the value of the property mediator as a child element in the second payload.根据我的理解,您希望将属性中介的值添加为第二个负载中的子元素。 Thus with the enrich mediator, we can achieve the following payload.因此,使用丰富的中介器,我们可以实现以下有效负载。

{
        "first": "response",
        "orderSource": "value"
 }

It seems that you want to add this generated payload as a value in another JSON element, so that I have used a payload factory mediator to achieve this use case.似乎您想将此生成的有效负载添加为另一个 JSON 元素中的值,以便我使用有效负载工厂中介器来实现此用例。 Thus this will result in the following payload after the payload factory mediation.因此,这将在有效载荷工厂调解之后产生以下有效载荷。

{
    "request": {
        "first": "response",
        "orderSource": "value"
    }
}

[1]-https://docs.wso2.com/display/EI600/JSON+Support [1]-https://docs.wso2.com/display/EI600/JSON+Support

Updated更新

I have modified the mediation to use the XPTH functions instead of the JSON path.我已修改中介以使用 XPTH 函数而不是 JSON 路径。 Please refer to the following sample configuration.请参考以下示例配置。

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="enrichProxy2"
       startOnLoad="true"
       statistics="disable"
       trace="disable"
       transports="http,https">
   <target>
      <inSequence>
         <property name="messageType"
                   scope="axis2"
                   type="STRING"
                   value="application/xml"/>
         <enrich>
            <source clone="true" xpath="$body//jsonObject/*"/>
            <target property="orderSource" type="property"/>
         </enrich>
         <call>
            <endpoint>
               <address uri="http://run.mocky.io/v3/9cf4b844-57c1-4fa5-a101-881dc36385bd"/>
            </endpoint>
         </call>
         <enrich>
            <source clone="false" property="orderSource" type="property"/>
            <target action="child" xpath="//jsonObject"/>
         </enrich>
         <enrich>
            <source clone="true" xpath="//jsonObject"/>
            <target type="body"/>
         </enrich>
         <respond/>
      </inSequence>
   </target>
   <description/>
</proxy>
           

Update 2更新 2

To prevent automatic data conversion you need to configure synapse.commons.json.output.autoPrimitive = false in [ESB_HOME]/repository/conf/synapse.properties file [2].为了防止自动数据转换,您需要在 [ESB_HOME]/repository/conf/synapse.properties 文件 [2] 中配置 synapse.commons.json.output.autoPrimitive = false。

[2]-https://www.yenlo.com/blog/wso2torial-json-magic-in-wso2-esb-5.0.0 [2]-https://www.yenlo.com/blog/wso2torial-json-magic-in-wso2-esb-5.0.0

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

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