java Zip 2在Spring Webfux中的不同Mono
我有下面的功能
public Mono<CarAndShip> getCarAndShip(Long id) {
Mono<Car> carMono = carService.getCar(id).subscribeOn(Schedulers.elastic());
Mono<Ship> shipMono = shipService.getShip(id).subscribeOn(Schedulers.elastic());
return Mono.zip(carMono, shipMono)
.flatMap(zipMono -> {
return new CarAndShip(zipMono.getT1(), zipMono.getT2());
});
IntelliJ在返回声明中抱怨:
Required type: Mono <CarAndShip>
Provided: Mono <Object> no
instance(s) of type variable(s) R exist so that CarAndShip conforms to Mono<? extends R>
如何将cast类型转换为所需的CarAndShip
返回类型
# 1 楼答案
例外情况是:你没有提供单声道
所以要么使用
map
而不是flatMap
:或者提供单声道(这里可能不是最好的方式):
# 2 楼答案
在本例中,您应该使用
map()
而不是flatMap()
,因为您没有返回Mono