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

标签 mono spring-webflux flux

我有一个返回 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/

相关文章:

c# - 使用 Mono 4.3.3 在 Debian/Jessie 上构建 MonoDevelop

spring-boot - SpringBoot2 + Webflux - WebTestClient 返回 "Content not available yet"

java - 使用 ReactiveCouchbaseRepository 时出现 IllegalArgumentException 期望找到类 rxSingle 的响应式(Reactive)适配器,但无法找到

java - 如何在 Spring 数据 r2dbc 中替换 @PrePersist

reactjs - 登录ajax请求Flux React?

MonoDevelop设置修复 "ran out of trampolines type 2"错误

c# - Linux Ubuntu 单声道

c# - 状态存储在 fluxor 中的什么位置?

c# - Mono Pkg-Config.exe 不断崩溃?

javascript - 使用 Flummox (Flux)、React 和 React-router 设计 CRUD 应用程序