我相信您可以使用如下中介来实现您的用例。
<?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]/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