gpt4 book ai didi

mono - 如何在 WebFlux 的 ServerResponse 中返回 Flux

转载 作者:行者123 更新时间:2023-12-05 01:54:35 30 4
gpt4 key购买 nike

我有一个返回 Flux 的 Jdbc 层。 fromPublisher 方法返回数据时,它正在接受其他可序列化类,但该方法不接受 Flux。

方法一

public Mono<ServerResponse> getNames(final ServerRequest request) {
Flux<String> strings = Flux.just("a", "b", "c");
return ServerResponse.ok().contentType(APPLICATION_JSON)
.body(fromPublisher(response), String.class);
}

以上方法是返回组合为单个字符串的 abc。

我试过了,

return ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(BodyInserters.fromValue(response), List.class);

我也试过了。

 Mono<List<String>> mono = response.collectList();
ServerResponse.ok()
.contentType(APPLICATION_JSON)
.body(fromPublisher(mono, String.class));

但这也给出了一个运行时错误

> body' should be an object, for reactive types use a variant specifying
> a publisher/producer and its related element type

enter image description here

最佳答案

下面是一个如何发回 Flux<String> 的例子在响应正文中

Flux<String> strings = Flux.just("a", "b", "c");
ServerResponse.ok().body(strings, String.class);

关于mono - 如何在 WebFlux 的 ServerResponse 中返回 Flux<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70605765/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com