3

added an ExchangeFilterFunction to WebClient which logs request and response but while logging, was unable to log request and response body as a string or JSON. It prints as an object

Tried different castings and retrieving the body as a string using bodyToMono, toEntity but they return an object and not string exactly.

        logRequest(clientRequest.url(), clientRequest.method(), clientRequest.headers(), clientRequest.body());
        Mono<ClientResponse> response = exchangeFunction.exchange(clientRequest);
        ClientResponse clientResponse = response.block();

      logResponse(clientResponse.statusCode(), clientResponse.headers(), clientResponse.toString(), clientResponse);

        return response;
    }```

```private void logRequest(URI uri, HttpMethod method, HttpHeaders httpHeaders, BodyInserter<?, ? super ClientHttpRequest> body) {

        log.info("Request: {}", 
                 LogVar.with("Body", body)
        );
    }

    private void logResponse(HttpStatus statusCode, Headers headers, String body, ClientResponse extractor) {
        log.info("Response: {}", 
                 , LogVar.with("Body", extractor.bodyToMono(String.class))
        );
    }```

Expecting Json OR XML whatever the request/response is, to be logged as a string.
Munni
  • 79
  • 1
  • 7
  • 1
    possible duplicate https://stackoverflow.com/questions/45240005/how-to-log-request-and-response-bodies-in-spring-webflux – Toerktumlare Nov 11 '19 at 23:40
  • It isn't a duplicate. The issue is related to WebClient but not to WebFlux WebFilters – ActivX Sep 02 '22 at 10:13

0 Answers0