title: How to log request and response bodies in Spring WebFlux
body: <p>I want to have centralised logging for requests and responses in my REST API on Spring WebFlux with Kotlin. So far I've tried this approaches</p>
  
  <pre><code>@Bean
  fun apiRouter() = router {
      (accept(MediaType.APPLICATION_JSON) and "/api").nest {
          "/user".nest {
              GET("/", userHandler::listUsers)
              POST("/{userId}", userHandler::updateUser)
          }
      }
  }.filter { request, next -&gt;
      logger.info { "Processing request $request with body ${request.bodyToMono&lt;String&gt;()}" }
      next.handle(request).doOnSuccess { logger.info { "Handling with response $it" } }
  }
  </code></pre>
  
  <p>Here request method and path log successfully but the body is <code>Mono</code>, so how should I log it? Should it be the other way around and I have to subscribe on request body <code>Mono</code> and log it in the callback?
  Another problem is that <code>ServerResponse</code> interface here doesn't have access to the response body. How can I get it here?</p>
  
  <hr>
  
  <p>Another approach I've tried is using <code>WebFilter</code></p>
  
  <pre><code>@Bean
  fun loggingFilter(): WebFilter =
          WebFilter { exchange, chain -&gt;
              val request = exchange.request
              logger.info { "Processing request method=${request.method} path=${request.path.pathWithinApplication()} params=[${request.queryParams}] body=[${request.body}]"  }
  
              val result = chain.filter(exchange)
  
              logger.info { "Handling with response ${exchange.response}" }
  
              return@WebFilter result
          }
  </code></pre>
  
  <p>Same problem here: request body is <code>Flux</code> and no response body.</p>
  
  <p>Is there a way to access full request and response for logging from some filters? What don't I understand?</p>