如何从 HTTP REST 调用中获取完整的 EntityResponse object?

How to get the full EntityResponse object from a HTTP REST call?

我正在尝试从对服务器的 REST 调用中获取完整的 ResponseEntity<String>

服务器的其余控制器如下所示:

    @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);
    }

我非常有信心通过这种方式让 ResponseEntity<String> 回来。

现在让我们看看我在接收端有什么。 客户端 看起来像这样:

@MessagingGateway
public interface MyClient {

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

当然是ClientConfiguration

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();
    }
}

通过这个设置,我最终得到一个空 ResponseEntity<String>。我打印出来了:

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

看起来不错,但是 body 是空的。我也打印了那个,它是 null(和 hasBody() = false

我已经尝试了很多,但运气不佳。仅获取有效负载(因此只有 ResponseEntity<String>String)很容易退出,因为这似乎是标准用法。我只是将客户端中的 return 类型从 ResponseEntity<String> 更改为 String 并将 expectedResponseType 作为 String 添加到我的集成流程中,如下所示,我获得有效载荷就好了:

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

所以我似乎可以在没有 body 的情况下获得 ResponseEntity 或仅获得 body,但不能同时获得两者。我做错了什么?

有一个新的合并请求来添加该功能。

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