Search code examples
spring-integrationspring-integration-dslspring-integration-http

Spring integration: http inbound gateway detect empty body and use 204 code


I have the following HTTP inbound gateway:

  @Bean
  public IntegrationFlow httpInbound() {
    return IntegrationFlows
        .from(Http.inboundGateway("/").requestMapping(m -> m.methods(HttpMethod.POST)))
        .channel(SOME_CHANNEL_NAME).get();
  }

And this service for the reply:

  @ServiceActivator(inputChannel = SOME_CHANNEL_NAME)
  public String receive() {
    //process string
    return string;
  }

But this is returning 200 even when the reply is empty (""). I would like a 204 status code when string is empty and 200 when string is not empty. Of course I can do this:

  @ServiceActivator(inputChannel = SOME_CHANNEL_NAME)
  public GenericMessage receive() {
    //process string
    return "".equals(string) ? new GenericMessage<>("", Map.of(HttpHeaders.STATUS_CODE, 204)) : new GenericMessage<>(string, Map.of(HttpHeaders.STATUS_CODE, 200));
  }

But I'm asking this because maybe there is an easier and cleaner way (maybe configuring the inbound gateway or using an http outbound gateway).

Thanks!


Solution

  • What you do is fully OK. Another way is return just a HttpStatus.NO_CONTENT as a payload. You also can return a whole ResponseEntity. You also can return null and set a replyTimeout() to some limited number. Then you can set a statusCodeFunction() to produce that HttpStatus.NO_CONTENT.