例如,假设我有一个 WebFilter,它写了一些 Context
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.contextWrite(Context.of("my-context", "foobar"));
}
在下游,我的控制器执行此操作
@GetMapping(path = "test")
public Mono<String> test() throws Exception {
final Mono<ContextView> contextMono = Mono.deferContextual(Mono::just);
return contextMono.flatMap(ctx -> Mono.just(ctx.get("my-context")));
}
以上所有工作正常。
如果我想从控制器方法返回一个 Single 怎么办?我尝试使用RxJava3Adapter.monoToSingle()
,但它破坏了反应器链。
@GetMapping(path = "test")
public Single<String> test() throws Exception {
final Mono<ContextView> contextMono = Mono.deferContextual(Mono::just);
return RxJava3Adapter.monoToSingle(
contextMono.flatMap(ctx -> Mono.just(ctx.get("my-context"))));
}
我的猜测是,由于我没有返回 Mono,因此 RxJava3Adapter 内没有任何内容订阅此 contextMono。这是正确的解释吗?
有没有办法在传入 Context 时返回 Single ?