1

I have next method that uses in AbstractWebClient. How can I refactor it to get rid of deprecated .exchange() and save availability to work in abstract way?

    private ClientResponse executeRequest(WebClient.RequestHeadersSpec<?> spec,
                                          RequestContext requestContext,
                                          String requestBody) {
        logRequest(requestContext, requestBody);
        return spec
                .exchange()
                .blockOptional()
                .orElseThrow(() -> new InternalException(String.format("%s: can't extract response from method \"%s\"", externalServiceName, requestContext.getApiMethodName())));
    }
Fedor Doronin
  • 31
  • 1
  • 6
  • Does this answer your question? [How to refactor WebClient.exchange in Spring 5.3 WebFlux?](https://stackoverflow.com/questions/71584519/how-to-refactor-webclient-exchange-in-spring-5-3-webflux) – Alex Jul 21 '22 at 09:35

1 Answers1

1

You can use retrieve() method as suggested in exchange() documentation something like below:

return 
spec.retrieve().bodyToMono(ClientResponse.class).blockOptional()
                .orElseThrow(() -> new InternalException(String.format("%s: can't extract response from method \"%s\"",
                        externalServiceName, requestContext.getApiMethodName())));
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
  • I don't think you can use `ClientResponse.class`, you need to provide some sort of concrete implementation for that interface. Is that implied in your answer? – zaf187 Jan 06 '23 at 12:27
  • .bodyToMono use class type for decoding purpose, we are not passing concrete class's implementation in it. Hope that helps @zaf187 – Ashish Patil Jan 06 '23 at 19:22