Skip to content
Advertisement

How to refactor the deprecated method .exchange()?

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

Advertisement

Answer

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())));
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement