在 MuleSoft 4+、Dataweave 2+ 中,如何清除有效负载?我想向客户端发送一个空的负载

In MuleSoft 4+, Dataweave 2+ How can I clear the payload ? I want to send the client an empty payload

我想 return 给客户端一个空的负载和一个 201 状态码。

如何清除Mule或Dataweave中的payload?我不想将它设置为 null 或空字符串。我只是希望它是空的。这可能吗?

对于 201 响应代码,您应该 returning body - 这是预期的。有关详细信息,请参阅此答案:Create request with POST, which response codes 200 or 201 and content

这就是为什么如果您 return 一个 201,即使回复中没有 body,postman 等工具仍然会向您显示空回复 body。例如,如果您将响应代码换成 204,则它不会显示响应 body.

这是一个示例应用程序,向您展示了如何设置响应代码。请注意,在我的转换中,我设置了一个名为 responseCode 的变量,并且在我的 HTTP 侦听器中,我将响应代码设置为 vars.responseCode default 200;这意味着如果我显式设置变量 responseCode 它将使用它,如果我不设置它将使用 200。尝试更改您的响应代码以查看它的外观。请注意,即使您确实设置了响应 body 但 return 204 状态代码,邮递员也不会显示 body。更改转换以将有效负载设置为 null,您将看到相同的行为 - 201 期待一个 body,因此它显示它得到一个空值 body,204 将不显示任何内容.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
    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:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="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/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="6694e527-1d09-4c26-bc6e-0173e3fc6d04" >
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>
    <flow name="response-code-testFlow" doc:id="eac8817d-8806-4b8f-98cd-5ef277ba71be" >
        <http:listener doc:name="Listener" doc:id="3ceb639d-d5cf-4933-8510-7cb6ca1b29fb" config-ref="HTTP_Listener_config" path="/test">
            <http:response statusCode="#[vars.responseCode default 200]" />
        </http:listener>
        <ee:transform doc:name="Transform Message" doc:id="92d9dda8-2bc5-4528-a052-74d4886de29a" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
    "message": "Hello world"
}]]></ee:set-payload>
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="responseCode" ><![CDATA[204]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
    </flow>
</mule>

你好,在 mule 4 中,您可以通过在流程末尾设置一个空负载来清除负载,并为 HTTP 状态代码 201 设置 HTTP 状态。 此外,您可以默认设置 HTTP 状态错误代码。

为此,您只需要在 HTTP 侦听器中添加一个配置,如下所示(您可以将其用于任何 API)

  <http:listener config-ref="HTTP_Listener_config" path="/api/*">
            **<http:response statusCode="#[vars.httpStatus default 200]">
                <http:headers>#[vars.outboundHeaders default {}]</http:headers>
            </http:response>**
            **<http:error-response statusCode="#[vars.httpStatus default 500]">
                <http:body>#[payload]</http:body>
                <http:headers>#[vars.outboundHeaders default {}]</http:headers>
            </http:error-response>**
        </http:listener>

其次,在流程末尾添加一个转换消息,如下所示,最终响应为空负载,HTTP 状态代码为 201

 <ee:transform xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{}]]></ee:set-payload>
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="httpStatus" ><![CDATA[%dw 2.0
output application/java
---
201]]></ee:set-variable>
            </ee:variables>
        </ee:transform>

谢谢