You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

reactor-core 3.1版本兼容性问题:原有代码无法编译如何修复?

Fixing the Reactor 3.1.0+ Compilation Issue with 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:35:25