0

I'm trying to get the full ResponseEntity<String> from a REST call to a server.

The rest controller of the server looks like this:

    @PostMapping(consumes = { "application/json;charset=UTF-8" })
    public ResponseEntity<String> post(@Validated @RequestBody final SomeParam param) {
        final String output = service.doStuff(param);
        return ResponseEntity.ok(output);
    }

I'm pretty confident I'm getting the ResponseEntity<String> back this way.

Now lets see what I have on the receiving end. The client looks like this:

@MessagingGateway
public interface MyClient {

    @Gateway(requestChannel = ClientConfiguration.REQUEST_CHANNEL, replyChannel = ClientConfiguration.REPLY_CHANNEL)
    ResponseEntity<String> makePost(@Header("url") String url, @Payload SomeParam param);
}

The ClientConfiguration of course:

public class ClientConfiguration{

    static final String REQUEST_CHANNEL = "requestChannel";

    static final String REPLY_CHANNEL = "replyChannel";

    @Bean(name = REQUEST_CHANNEL)
    MessageChannel requestChannel() {
        return MessageChannels.direct(RENDERER_REQUEST_CHANNEL).get();
    }

    @Bean(name = REPLY_CHANNEL)
    MessageChannel replyChannel() {
        return MessageChannels.direct(RENDERER_REPLY_CHANNEL).get();
    }

    @Bean
    MessageChannel channel() {
        return MessageChannels.direct().get();
    }

    @Bean
    IntegrationFlow channelFlow() {
        return IntegrationFlows.from(channel())
                .handle(Http.outboundGateway("url", new RestTemplate())
                        .charset("UTF-8"))
                .channel(replyChannel())
                .get();
    }
}

With this setup I end up getting an empty ResponseEntity<String>. I printed it:

<200,[Date:"Wed, 31 Mar 2021 15:53:22 GMT", Content-Type:"text/html;charset=utf-8", Content-Length:"8732"]>

It looks fine but the body is however empty. I printed that one also out and it was null (and hasBody() = false)

I've experimented quite a bit without luck. Getting the payload only (so only the String of the ResponseEntity<String>) is quit easy as it seems is the standard usage. I just change the return type in my Client from ResponseEntity<String> to String and add the expectedResponseType as being a String in my integration flow as follows, and I get the payload just fine:

    IntegrationFlow channelFlow() {
        return IntegrationFlows.from(channel())
                .handle(Http.outboundGateway("url", new RestTemplate())
                        .charset("UTF-8")
                        .expectedResponseType(String.class)) // <- this
                .channel(replyChannel())
                .get();
    }

So it seems I can get the ResponseEntity without the body or just the body, but never both. What am I doing wrong?

crizzis
  • 9,978
  • 2
  • 28
  • 47
Younes El Ouarti
  • 2,200
  • 2
  • 20
  • 31

1 Answers1

1

There is a new Pull Request to add that capability.

https://github.com/spring-projects/spring-integration/pull/3530

Gary Russell
  • 166,535
  • 14
  • 146
  • 179