gpt4 book ai didi

java - 方法引用 : cannot convert reactor. core.publisher.Mono 到reactor.core.publisher.Mono

转载 作者:行者123 更新时间:2023-12-02 01:49:54 24 4
gpt4 key购买 nike

我有两个Mono -s,如果一个不为空(用户已经存在),我想返回/抛出异常,或者返回 Mono<User>基于另一值的值(将设置和 DTO 数据复制到用户对象)。我写道:

public Mono<User> registerUser(UserRegistrationDto userRegistrationDto) {
Mono<Settings> settingsMono = settingsService.getSettings();
Mono<User> dbUserMono = userRepository.findByEmail(userRegistrationDto.getEmail());
return dbUserMono.map(dbUser -> Mono.error(new AlreadyRegisteredException())).or(
settingsMono.map(
settings -> validateDtoAndCreateUser(userRegistrationDto, settings)
).flatMap(userRepository::save)
);

userRepository 是一个 Spring Data ReactiveMongoRepository。 validateDtoAndCreateUser 的签名是:private User validateDtoAndCreateUser(UserRegistrationDto userRegistrationDto, SiteSettings siteSettings)

不幸的是userRepository::save我得到:

Bad return type in method reference: cannot convert reactor.core.publisher.Mono<S> to reactor.core.publisher.Mono<? extends R>

我做错了什么?我是否在某个地方使用了错误的运算符?

最佳答案

这样做:

return dbUserMono.flatMap(dbUser -> Mono.<User>error(new AlreadyRegisteredException()))
.switchIfEmpty(settingsMono.map(
settings -> validateDtoAndCreateUser(userRegistrationDto, settings)
).flatMap(userRepository::save));

关于java - 方法引用 : cannot convert reactor. core.publisher.Mono<S> 到reactor.core.publisher.Mono<? 中的返回类型错误延伸R>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com