What are the benefits of having Mono as parameter in HTTP endpoint handler methods?
Let's say I have this code:
public Mono<ResponseDto> handle (@RequestBody Mono<RequestBodyDto> requestBody) {
return requestBody
.flatMap(service::doSomething)
.doOnError(%printError with requestBody%);
}
I want to log the request in an error log, but I do not have access to it in error handling lambda. I can't call requestBody.block().
Why I can't just do this:
public Mono<ResponseDto> handle (@RequestBody RequestBodyDto requestBody) {
return Mono.just(requestBody)
.flatMap(service::doSomething)
.doOnError(%printError with requestBody%);
}
?
In this case I have access to requestBody via the method parameter.
Reactive programming caters for high performance asynchronous data streaming. If you serialize the payload just for logging purposes, you defeat this optimization and risk memory leaks. You can log meta-data though.
If you still need to log request or response bodies, you might consider using a WebFilter, see here for an example: How to log request and response bodies in Spring WebFlux