问题描述
我尝试使用
<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>
在这里,我需要向现有有效负载添加一个名为orderSouce的元素。如何插入该元素。 现有有效载荷是一个json请求。 我正在捕获orderSource的值,但是没有获取如何插入元素orderSource和值。
解决方法
我相信您可以使用以下中介来实现用例。
<?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>
让我解释一下我在这里所做的事情。由于您正在处理JSON有效负载,因此请在中介中使用JSON路径[1]而不是XPATH。这样可以避免在中介中进行不必要的数据转换。
我们正在使用以下负载调用上述代理服务。
{"orderSource":"value"}
我们正在使用JSON路径捕获此有效负载,并将值存储在属性mediator orderSource中。然后,我们进行端点调用,它将返回以下JSON有效负载。
{"first":"response"}
根据我的理解,您想将属性中介器的值添加为第二个有效负载中的子元素。因此,使用Rich Mediator,我们可以实现以下有效负载。
{
"first": "response","orderSource": "value"
}
似乎您想将此生成的有效负载作为值添加到另一个JSON元素中,因此我已使用有效负载工厂中介程序来实现此用例。因此,这将在有效负载工厂中介之后产生以下有效负载。
{
"request": {
"first": "response","orderSource": "value"
}
}
[1] -https://docs.wso2.com/display/EI600/JSON+Support
已更新
我已修改中介以使用XPTH函数而不是JSON路径。请参考以下示例配置。
<?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>
更新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