'Why does using 'Optional<>' give me the next error'Failed to convert from Optional to Mono?

I am doing a functionality that allows you to save users, but before that it does a validation to validate if the nick exists in the database, when I run I get the following error:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.Optional<?>] to type [reactor.core.publisher.Mono<?>] for value 'Optional[FluxUsingWhen]'; nested exception is 
java.lang.ClassCastException: class java.util.Optional cannot be cast to class reactor.core.publisher.Flux (java.util.Optional is in module java.base of loader 'bootstrap'; reactor.core.publisher.Flux is in unnamed module of loader 'app')] with root cause

java.lang.ClassCastException: class java.util.Optional cannot be cast to class reactor.core.publisher.Flux (java.util.Optional is in module java.base of loader 'bootstrap'; reactor.core.publisher.Flux is in unnamed module of loader 'app')

Error log

My classes: Controller

@Slf4j
@RestController
@RequestMapping("/api/uzer")
@RequiredArgsConstructor
public class PruebaController {
    private final UserService service;

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public Mono<ResponseEntity> saveUser(@Valid @RequestBody UserRequest userRequest) {
        String name = userRequest.getNick();

        return service.findByNick(name)
                .map(exist -> {
                    if (exist.isPresent()) {
                        throw new NotFoundResourceException("ERR-450", "exist user: ");
                    }
                    return new ResponseEntity<>(service.saveUser(userRequest), HttpStatus.CREATED);
                });
    }
}

Service

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserRequestToUser mapper;
    public Mono<Optional<User>> findByNick(String nick){
        return userRepository.findFirstByNick(nick);
    };
    public Mono<User> saveUser(UserRequest userRequest){
        User user = mapper.map(userRequest);
        return userRepository.save(user);
    }

Repository

@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String> {

    Mono<Optional<User>> findFirstByNick(String nick);
    Mono<User> findUserByNick(String nick);
}

Model

@Data
@Document
@ToString
public class User {
    @Id
    private String id;
    @NotNull
    @NotEmpty
    @Indexed(unique=true)
    private String nick;
    @NotBlank
    @NotNull
    @Pattern(regexp = "123|321")
    private String password;
    @NotNull
    private boolean userStatus;


    public User() {
    }

    public User(String id, String nick, String password, boolean userStatus) {
        this.id = id;
        this.nick = nick;
        this.password = password;
        this.userStatus = userStatus;
    }
}

Please, if you consider that there are better options to do this validation with another code, leave it also as an example to learn.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source