方法参考中的返回类型错误:无法将reactor.core.publisher.Mono <s>转换为reactor.core.publisher.Mono <!-?扩展R->

时间:2018-11-04 07:43:11

标签: java spring-webflux project-reactor

我有两个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>

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

1 个答案:

答案 0 :(得分:2)

这样做:

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