0

我想在 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>
4

2 回答 2

1

编辑。

这是因为 dataweave 脚本的第一部分将负载覆盖到 JSON。因此,当您在 set-property dw 脚本中使用 sizeOf 时,它实际上是在对不为 0 的 JSON 字符串执行 sizeOf。

为了完成这项工作,我可能会将原始有效负载存储在 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

于 2015-10-23T14:47:57.353 回答
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>

我确实喜欢这样:当payload.customer ==“未找到”时“404”

于 2015-10-24T06:34:29.603 回答