传入有效负载:
{
"Categories": [
{
"ID": "5a873ca3",
"Code": "CTY"
}, {
"ID": "89k873c8",
"Code": "CTY"
}
]
}
flowVar 值是一个 ArrayList: ([84hkj569],[6j93hl9](
所需的输出有效载荷:
{
"Categories": [
{
"ID": "5a873ca3",
"Code": "CTY"
}, {
"ID": "89k873c8",
"Code": "CTY"
}, {
"ID": "84hkj569",
"Code": "CTY"
}, {
"ID": "6j93hl9",
"Code": "CTY"
}
]
}
我找不到在数据编织中做的方法, 请你帮忙
下面的数据编织代码应该能给你你想要的:
%dw 1.0
%output application/json
---
{
Categories: payload.Categories ++ (flowVars.value map {
"ID": $,
"Code": "CTY"
})
}
这是我使用的示例流中的配置和输出:
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"Categories": [
{
"ID": "5a873ca3",
"Code": "CTY"
}, {
"ID": "89k873c8",
"Code": "CTY"
}
]
}]]></dw:set-payload>
</dw:transform-message>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[
flowVars.value = new java.util.ArrayList();
flowVars.value.add("84hkj569");
flowVars.value.add("6j93hl9");
return payload;
]]></scripting:script>
</scripting:component>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
Categories: payload.Categories ++ (flowVars.value map {
"ID": $,
"Code": "CTY"
})
}]]></dw:set-payload>
</dw:transform-message>
输出:
{
"Categories": [
{
"ID": "5a873ca3",
"Code": "CTY"
},
{
"ID": "89k873c8",
"Code": "CTY"
},
{
"ID": "84hkj569",
"Code": "CTY"
},
{
"ID": "6j93hl9",
"Code": "CTY"
}
]
}
Brad Cooper的dataweave代码来连接JSON有效载荷和flowVars.value没有任何问题。以下是适用于3.8.1的完整示例。
完整代码:
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="sample-dataweaveFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/sample" doc:name="HTTP"/>
<logger message="Request Received" level="INFO" doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-variable variableName="value"><![CDATA[%dw 1.0
%output application/java
---
['84hkj569', '6j93hl9']]]></dw:set-variable>
</dw:transform-message>
<logger message="Flow Variable: #[flowVars.value]" level="INFO" doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
Categories: [
{
"ID": "5a873ca3",
"Code": "CTY"
}, {
"ID": "89k873c8",
"Code": "CTY"
}
]
}]]></dw:set-payload>
</dw:transform-message>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="Payload: #[payload]" level="INFO" doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
Categories: payload.Categories ++ (flowVars.value map {
ID: $,
Code: 'CTY'
})
}]]></dw:set-payload>
</dw:transform-message>
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="Final Response: #[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
测试网址:
http://localhost:8081/sample
控制台输出:
INFO 2018-05-09 01:08:51,575 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Request Received
INFO 2018-05-09 01:08:51,576 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
INFO 2018-05-09 01:08:51,595 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Flow Variable: [84hkj569, 6j93hl9]
INFO 2018-05-09 01:08:51,596 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] com.mulesoft.weave.mule.utils.MuleWeaveFactory$: MimeType was not resolved '*/*' delegating to Java.
INFO 2018-05-09 01:08:51,620 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Payload: {
"Categories": [
{
"ID": "5a873ca3",
"Code": "CTY"
},
{
"ID": "89k873c8",
"Code": "CTY"
}
]
}
INFO 2018-05-09 01:08:51,635 [[sample-dataweave].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Final Response: {
"Categories": [
{
"ID": "5a873ca3",
"Code": "CTY"
},
{
"ID": "89k873c8",
"Code": "CTY"
},
{
"ID": "84hkj569",
"Code": "CTY"
},
{
"ID": "6j93hl9",
"Code": "CTY"
}
]
}