spring - 在请求写入完成之前,WebClient 不会读取响应。

标签 spring webclient spring-webflux reactor-netty

我正在尝试实现流代理。
我遇到了 Spring 响应式(Reactive) WebClient 的问题。

任何人都可以帮助我理解我是否以某种错误的方式或它只是来自 WebClient 方面的错误?

堆栈:

react 器网络 0.7.8.RELEASE

spring-boot 2.0.4.RELEASE

描述:

我想将一个长流代理到外部服务,然后将响应流转发给请求者。
流式传输使用块(HTTP 1.1 Transfer-Encoding : chunked)。
外部服务处理每个块并发送到响应结果。

预期行为:

WebClient 应立即读取响应的每个接收部分。

实际行为:

在请求写入完成之前,WebClient 不会启动进程响应。

代码:

return client
    .post()
    .header("Transfer-Encoding", "chunked")
//because I want to flush each received part
    .body((outputMessage, context) -> outputMessage.writeAndFlushWith(
        request.body(BodyExtractors.toDataBuffers())
               .map(dataBuffer -> Mono.just(dataBuffer))))
    .exchange()
    .flatMap(clientResponse -> {
      ServerResponse.BodyBuilder bodyBuilder = ServerResponse.status(clientResponse.statusCode());
      bodyBuilder.contentType(MediaType.APPLICATION_STREAM_JSON);

      return bodyBuilder.body((outputMessage, context) ->                                                        
          outputMessage.writeAndFlushWith(                                               
            clientResponse.body(BodyExtractors.toDataBuffers())                                                               
                          .map(dataBuffer -> Mono.just(dataBuffer))
                         ));}
);

最佳答案

刚刚测试了基于 Jetty 的 WebClient 实现,它的行为符合您的预期。它可以在发送所有请求内容之前开始读取响应。
它应该已经在 Spring Framework 5.1 中发布了
WebClient on Jetty new feature issue

关于spring - 在请求写入完成之前,WebClient 不会读取响应。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52113235/

相关文章:

WCF REST 服务 : Method parameter (object) is null

java - 如何在 Spring WebClient 中捕获 ConnectionException?

java - 获取 IgniteCheckedException : Default Ignite instance has already been started exception when enabling Persistence on single Node

java - Cloudfoundry spring java 7

Spring @Async 注解

c# - 错误请求 400 - 有效 URL 上的协议(protocol)错误 - Web 客户端

java - Windows 客户端与 Web 客户端

java - WebFlux : how to work takeUntilOther() method?

java - 如何使用 webflux 处理 Spring Boot 2 中的 HTTP OPTIONS 请求?

java - "merge"两个ApplicationContext配置的最佳方法