reactor-core 3.1版本兼容性问题:原有代码无法编译如何修复?
Mono.and() Great catch! This breaking change in Mono.and() is definitely a tricky one when upgrading from Reactor 3.0.x to 3.1.x. Here's how to adjust your code to work correctly in newer versions:
The Root Cause
In Reactor 3.0.7, Mono.and() was designed to combine two Mono instances and emit their results as a Tuple2. But starting in 3.1.0, the method's semantics changed entirely: it now only signals when both Monos complete successfully, returning a Mono<Void> with no payload. This means your code that tries to access getT1() and getT2() on the result will fail to compile.
The Fix: Use Mono.zip() Instead
To keep the behavior of combining the group key and its count into a tuple, replace and() with Mono.zip(). The zip() method is explicitly meant to merge results from multiple Monos into a tuple (or a custom object if you use the overload with a mapper).
Here's the revised code:
Flux.just("alpha", "bravo", "charlie") .map(String::toUpperCase) .flatMap(s -> Flux.fromArray(s.split(""))) .groupBy(String::toString) .sort((o1, o2) -> o1.key().compareTo(o2.key())) .flatMap(group -> Mono.zip(Mono.just(group.key()), group.count())) .map(keyAndCount -> keyAndCount.getT1() + " => " + keyAndCount.getT2()) .subscribe(System.out::println);
Why This Works
Mono.zip(Mono.just(group.key()), group.count()) returns a Mono<Tuple2<String, Long>>, which matches the type your subsequent map() operation expects. This maintains the original logic of pairing each character key with its occurrence count, while adhering to Reactor's updated API semantics.
内容的提问来源于stack exchange,提问作者Dean Schulze

