我想在Dataweave转换中设置为http.status代码。为此,我使用以下代码在http.status
标签上设置Outbound Property
:
出境物业===> http.status
%dw 1.0
%output application/java
---
"404" when payload[0] == null
otherwise "200"
但即使有效负载为空,也不会反映出来。可以有任何建议吗?
修改
此处参考是完整的Dataweave代码
<dw:transform-message doc:name="buildResponse">
<dw:set-payload>
<![CDATA[%dw 1.0
%input payload application/java
%output application/json
---
{
"customer": {
"id": payload[0].ID,
"name": payload[0].NAME,
"age": payload[0].AGE,
"address": {
"line1": payload[0].LINE1,
"line2": payload[0].LINE2,
"city": payload[0].CITY,
"state": payload[0].STATE,
"pincode": payload[0].PINCODE
}
}
} when (sizeOf payload) > 0
otherwise
{
"customer" : "not found"
}]]>
</dw:set-payload>
<dw:set-property propertyName="http.status">
<![CDATA[%dw 1.0
%output application/java
---
"404" when (sizeOf payload) == 0
otherwise "200"]]>
</dw:set-property>
</dw:transform-message>
答案 0 :(得分:1)
EDIT。
这是因为dataweave脚本的第一部分是将有效负载覆盖到JSON。所以当你在set-property dw脚本中使用sizeOf时,它实际上是在一个不是0的JSON字符串的大小。
为了使这项工作,我可能会将原始有效负载存储在flowVar中,并在dw脚本中使用它,如下所示:
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8085" doc:name="HTTP Listener Configuration" />
<flow name="dataweave-testFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/test" doc:name="HTTP" />
<set-payload value="#[[]]" doc:name="Set Payload" />
<set-variable variableName="originalPayload" value="#[payload]"
doc:name="Variable" />
<dw:transform-message doc:name="buildResponse">
<dw:set-payload>
<![CDATA[%dw 1.0
%input payload application/java
%output application/json
---
{
"customer": {
"id": payload[0].ID,
"name": payload[0].NAME,
"age": payload[0].AGE,
"address": {
"line1": payload[0].LINE1,
"line2": payload[0].LINE2,
"city": payload[0].CITY,
"state": payload[0].STATE,
"pincode": payload[0].PINCODE
}
}
} when (sizeOf payload) > 0
otherwise
{
"customer" : "not found"
}]]>
</dw:set-payload>
<dw:set-property propertyName="http.status">
<![CDATA[%dw 1.0
%output application/java
---
"404" when (sizeOf flowVars.originalPayload) == 0
otherwise "200"]]>
</dw:set-property>
</dw:transform-message>
</flow>
我认为这是Dataweave的一个错误,并在此处创建:https://www.mulesoft.org/jira/browse/MULE-9021
答案 1 :(得分:0)
<dw:transform-message doc:name="buildResponse">
<dw:set-payload>
<![CDATA[%dw 1.0
%input payload application/java
%output application/json
---
{
"customer": {
"id": payload[0].ID,
"name": payload[0].NAME,
"age": payload[0].AGE,
"address": {
"line1": payload[0].LINE1,
"line2": payload[0].LINE2,
"city": payload[0].CITY,
"state": payload[0].STATE,
"pincode": payload[0].PINCODE
}
}
} when (sizeOf payload) > 0
otherwise
{
"customer" : "not found"
}]]>
</dw:set-payload>
<dw:set-property propertyName="http.status">
<![CDATA[%dw 1.0
%output application/java
---
"404" when payload.customer == "not found"
otherwise "200"]]>
</dw:set-property>
</dw:transform-message>
我确实喜欢这样:&#34; 404&#34;当payload.customer ==&#34;未找到&#34;
时