为什么这个简单的 Webflux Controller 调用 Webclient 检索方法两次?

Ahm*_*aab 5 java kotlin spring-boot project-reactor spring-webflux

我有一个非常简单的 Webflux 控制器,它只向另一个服务端点发出 GET 请求并返回一个简单的 JSON 列表。问题是远程端点总是被调用两次。

如果我使用 Mono 而不是 Flux 作为控制器的返回类型,则不会发生此问题!

// This calls "/remote/endpoint" twice!

@GetMapping("/blabla")
fun controller() : Flux<JsonNode> {
    return webClient.get()
            .uri("/remote/endpoint")
            .retrieve()
            .bodyToMono(JsonNode::class.java)
            .flatMapIterable { body ->
                body.get("data")
            }
}

// This calls "/remote/endpoint" once.

@GetMapping("/blabla")
fun controller() : Mono<JsonNode> {
    return webClient.get()
            .uri("/remote/endpoint")
            .retrieve()
            .bodyToMono(JsonNode::class.java)
            .map { body ->
                body.get("data")
            }
}
Run Code Online (Sandbox Code Playgroud)