Is it possible to return a 204 from a Spring Integ

2019-07-21 23:42发布

问题:

I have an inbound-channel-adapter that forwards to a bean method that returns null, but since http has to return a response, a 200 is returned. There are cases where I'd like to specify what the return value is, e.g. a 204 and I've tried a bunch of things, but nothing seems to work.

I'm using spring-integration 3.0.2.

Thanks, Justin

回答1:

Yes, set the http_statusCode header in the reply message to 204.

EDIT: (see comments below)

<int-http:inbound-gateway request-channel="receiveChannel"
                      path="/receiveGateway"
                      supported-methods="POST"/>

<int:publish-subscribe-channel id="receiveChannel" task-executor="exec" />

<int:service-activator input-channel="receiveChannel" expression="payload + ' from the other side'" output-channel="next"/>

<int:chain input-channel="receiveChannel">  
    <int:header-enricher>
            <int:header name="http_statusCode" value="204"/>
    </int:header-enricher>
    <int:transformer expression="''"/>
</int:chain>

Assumes the main flow doesn't return a result.

If you want to wait until the flow completes, remove the task executor.

You probably also need a transformer, to transform the payload to "".