I'm new to Spring Reactor, so i want to refactor this simply spring data (on kotlin) method:
fun save(user: User): Mono<User> {
if (findByEmail(user.email).block() != null) {
throw UserAlreadyExistsException()
}
user.password = passwordEncoder.encode(user.password)
return userRepository.save(user)
}
Thanks
Something like this should work:
Notice I'm using MicroUtils/kotlin-logging. Delete the log statements if you don't know or just don't want them.
Basically, you need to "consume" (a.k.a. subscribe) first to what's coming in the
ServerRequestin order to access the content.Alternatively, instead of throwing an exception, you can also have an actual flow that handles that scenario; something like: