Spring WebFlux: Emit exception upon null value in Spring Data MongoDB reactive repositories?

Viewed 9145

I'm trying to learn how to use MongoDB reactive repositories using spring-boot 2.0.0.M2, but I fear that I'm not doing things as intended.

This is one of my methods, which tries to find a User by their email. But if there is none, the method should throw an exception.

@Override
public Mono<User> findByEmail(String email) {
    User user = repository.findByEmail(email).block();
    if(user == null) {
        throw new NotFoundException("No user account was found with email: " + email);
    }
    return Mono.just(user);
}

The repository extends ReactiveCrudRepository<User, String>, but I fear that by using .block() I'm preventing this method from being reactive. I'm new to reactive programming, and I'm having a hard time to find good documentation on it. Can someone please point me in the right direction?

1 Answers
Related