I'm trying to make WebClient
return an Optional.empty()
when I get a 404, not found, from the server. But instead I get a Optional with a User
object with all properties set to null.
What am I missing?
@Override
public Optional<User> getUser(Username username) {
return webClient
.get()
.uri(buildUrl(username))
.retrieve()
.onStatus(HttpStatus.NOT_FOUND::equals, response -> Mono.empty())
.onStatus(HttpStatus::is4xxClientError, response -> createError(response, CLIENTERROR))
.onStatus(HttpStatus::is5xxServerError, response -> createError(response, SERVRERROR))
.bodyToMono(User.class)
.blockOptional();
}