无法使用Webflux返回大型JSON响应-netty和jetty均失败

问题描述 投票:1回答:1

我想从webflux端点返回一个较大的(3到9 Mb之间)json正文。在这一点上,我只是想将其作为Mono<ResponseEntity<String>>返回-完全绕过任何序列化/反序列化。但是,在大多数情况下,它仅返回实际字符​​串的一部分。 Content-Type是正确的,但是发送的字节不匹配-通常相差一Mb或更多,但肯定短了几百k。

[您将在下面看到正在设置Content-Type(在较低级别的某个位置),因此未将其分块发送。由于默认是分块,为什么会发生这种情况?发生在网状或码头上。

当我在发送ResponseEntity之前输出标头时,它显示传输编码:已分块。但是,响应头(在curl,chrome,Python请求库中)都显示Content-Length,而不是分块。

// IN CONTROLLER
log.info(response.getHeaders());
return Mono.just(new ResponseEntity<>(findByApplicationId(id), HttpStatus.OK));

这将记录以下[transfer-encoding:"chunked"]

// IN TERMINAL WITH CURL
MBP:my-test em$ curl -v localhost:8888/api/thing/0  > /dev/null
* TCP_NODELAY set
* Connected to localhost (::1) port 8888 (#0)
> GET /api/thing/0 HTTP/1.1
> Host: localhost:8888
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Content-Length: 5198562
<
{ [16308 bytes data]
100 5076k  100 5076k    0     0  16.0M      0 --:--:-- --:--:-- --:--:-- 16.0M
* Connection #0 to host localhost left intact

为什么在上面设置了Content-Length?分块到哪里去了?

通常会看到报告的Content-Length与实际接收到的字符串不匹配(显然是短截了)

这是我的完整POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>bugfix-group</groupId>
    <artifactId>webflux-jsonb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webflux-jsonb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
spring-boot spring-webflux embedded-jetty reactor-netty
1个回答
0
投票

基于经验测试,我有理由相信这是Spring中的错误。您可以在这里评估我的发现:

(问题已提交给Spring)https://github.com/spring-projects/spring-framework/issues/24128

(演示问题的存储库)https://github.com/opentable/webflux-large-response-spring

一些问题:

  • 您是否正在使用Jetty服务器或其他服务器(例如Reactor Netty)?
  • [当能够重现问题时(例如在IDE中本地,在Docker容器中等),您正在什么环境中运行应用程序?
  • 截取数据的响应大小是否一致?
© www.soinside.com 2019 - 2024. All rights reserved.