最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Spring webflux webclient inserting empty body for get requests - Stack Overflow

programmeradmin6浏览0评论

I am using webclient to integrate with external get API

WebClient webClient = WebClient.builder().filter(logRequestAndResponse()).baseUrl("url").build();
return webClient.get()
    .uri(uriBuilder -> uriBuilder.path("path").queryParams(validatedRequestParams).build())
    .headers(headers -> { headers.addAll(httpHeaders); })
    .retrieve()
    .toEntity(ResponseClass.class)
    .block();

public ExchangeFilterFunction logRequestAndResponse() {
    AtomicReference<String> startTime = new AtomicReference<>();
    ExchangeFilterFunction logRequest = ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
        log.info("##### Request Body: {}", ObjectMapperHelper.mapToString(clientRequest.body()));

        return Mono.just(clientRequest);
    });

                    return logRequest.andThen((clientRequest, next) -> next.exchange(clientRequest)
                            .flatMap(clientResponse -> clientResponse.bodyToMono(String.class)
                                    .defaultIfEmpty("")
                                    .flatMap(body -> {
        log.info("###response in: {}", body);
        return Mono.just(clientResponse.mutate().body(body).build());
                                    })
                            )

The issue is this always results in an empty body payload {} even though no body is explicitly added, which results in receiving an error as the external API is not expecting a body for the get request.

I tried to remove the content-length header, setting it to 0, but I got the same error. How can I prevent an empty payload from being inserted for the get request?

发布评论

评论列表(0)

  1. 暂无评论