3

向 WebClient 添加了一个 ExchangeFilterFunction,它记录请求和响应,但在记录时,无法将请求和响应正文记录为字符串或 JSON。它作为一个对象打印

尝试了不同的转换并使用 bodyToMono、toEntity 将主体作为字符串检索,但它们返回的是一个对象而不是字符串。

        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.
4

0 回答 0