我有一个返回 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
最佳答案
下面是一个如何发回 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/