Home > Enterprise >  How to refactor the deprecated method .exchange()?
How to refactor the deprecated method .exchange()?

Time:07-22

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())));
    }

CodePudding user response:

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())));
  • Related