I'm trying to figure out how to log a Mono<String> password with slf4j but it would always return a Monotype.
logger.info(login.getPassword() " " userRepository.findPasswordByUsername(login.getUsername()));
and
logger.info(login.getPassword() " " userRepository.findPasswordByUsername(login.getUsername()).toString());
the first 2 logging tries above
return the literal password (from the request) and MonoNext
and ofc you cant use .block()
which just throws
"block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3"
yes i'm aware that i can pass a Subscriber / Consumer for onNext() like:
.subscribe(x -> System.out.println( x.toString()))
to get some output but how would i do that with a logger only, is there even a way ?
login represents a user retrieved from a request.
The password is properly stored and encoded (Bycrypt) beforehand ofc (doesn't seem to be the issue).
Using spring-boot-starter-webflux, spring-boot-starter-data-r2dbc, spring-boot-starter-security
What am i missing ? Thanks in Advance.
CodePudding user response:
that is correct you should not block in a reactive application, neither should you subscribe in this usercase, as your application is most likely a producer, and the calling client is the consumer that subscribes.
what you are looking for is the doOn operators, that handles side effects. Logging is a side effect, its something you want to do on the side without disturbing the current flow. For instance update something, increment something, or in your case write to a log.
what you want os probably the doOnSuccess operator
example (i have not chacked against a compiler since im on mobile), but something like this.
function Mono<Login> foobar(Login login) {
return userRepository.findPasswordByUsername(login.getUsername)
.doOnSuccess(pwd -> {
logger.info(login.getPassword() " " pwd);
}).thenReturn(login);
}
CodePudding user response:
Reactive programming requires a different way of thinking about flow of data.
As you mentioned, you should never block as it goes against the principles of reactive programming.
I would start by reading this https://www.baeldung.com/java-string-from-mono for your specific question.
