更新时间:2023-11-06 18:46:58
我做错了什么?
目前尚不清楚您要实现的目标,但可能有以下几点:
Flux
返回 0..n 个字符串的流,您是否肯定只想要它返回的第一个字符串(这就是 next()
是吗?)Mono
或 Flux
.(订阅只有在您不使用框架并自己管理整个反应链 + 事件循环时才真正有效.)doOnNext(System.out::print)
作为反应链的一部分来实现这一点.Related to this question to take a string from a Mono I think I have finally got how to take a String
out of a Flux<String>
like this:
Flux<String> rates = controller.getRate(json);
String myRate = rates.next().subscribe(System.out::print);
However, this is causing a type mismatch cannot convert from Disposable to String
What am I getting wrong?
To clarify, it's for testing - I need an internal String value. So if I have a Flux<String>
of strings: one
, two
, three
then I want to set another String to value one
String s = ...
If I try
String s = rates.doOnNext(System.out::print);
Then I get a type mismatch: cannot convert from Flux<String> to String
If I try
String s = next().flatMap(System.out::print);
Then I get a type mismatch: cannot convert from Mono<Object> to String
What am I getting wrong?
It's not clear what you're trying to achieve, but potentially a few things here:
Flux<String>
returns a stream of 0..n strings, do you definitely only ever want the first one it returns (which is what next()
does?)Mono
or a Flux
on (for example) your controller method. (Subscribing is only really valid if you're not using a framework, and managing the entirety of the reactive chain + event loop yourself.)doOnNext(System.out::print)
as part of the reactive chain to achieve this.