我试着用
<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和值。
发布于 2020-09-19 22:27:03
我相信您可以使用以下中介来实现您的用例。
<?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路径捕获此有效负载,并将值存储在属性中介器orderSource中。然后进行端点调用,它将返回以下JSON有效负载。
{"first":"response"}
根据我的理解,您希望在第二个有效负载中添加属性中介器作为子元素的值。因此,有了丰富的中介,我们可以实现以下有效载荷。
{
"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/存储库/conf/syapse.properties文件2中配置synapse.commons.json.output.autoPrimitive = false。
2-https://www.yenlo.com/blog/wso2torial-json-magic-in-wso2-esb-5.0.0
https://stackoverflow.com/questions/63966392
复制相似问题