简体   繁体   中英

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

I have two Mono -s and I'd like to return/throw an exception if one wasn't empty (the user already exists), or return a Mono<User> value based on the other one (copy settings and DTO data to User object). I wrote:

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)
            );

The userRepository is a Spring Data ReactiveMongoRepository. The validateDtoAndCreateUser's signature is: private User validateDtoAndCreateUser(UserRegistrationDto userRegistrationDto, SiteSettings siteSettings)

Unfortunately at userRepository::save I'm getting:

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

What am I doing wrong? Am I using a bad operator somewhere?

Do it like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM