37

I have started using WebClient in my Spring boot project recently. Can somebody throw some light on the differences/usages between exchange and retrieve methods in WebClient.

I understand that exchange returns Mono<ClientResponse> and retrieve returns ResponseSpec, I just want to know when/why I should use each one of them.

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78

3 Answers3

33

Adding to @JArgente's answer.

According to the official documentation of the retrieve() method:

Perform the HTTP request and retrieve the response body.

...

This method is a shortcut to using exchange() and decoding the response body through ClientResponse.

and the exchange() method

Perform the HTTP request and return a ClientResponse with the response status and headers. You can then use methods of the response to consume the body:


The retrieve() method decodes the ClientResponse object and hands you the ready-made object for your use. It doesn't have a very nice api for handling exceptions.

However on the other hand the exchange() method hands you the ClientResponse object itself along with the response status and headers. With exchange method you get fine grained control over your response objects and a better way to handle the response object and the exceptions.

If you just want to consume some api go with retrieve().

If you want a better control over your response objects, headers and exceptions, go with exchange().


Update 1

Starting from Spring 5.3, the exchange() method is deprecated due to possible memory/connection leaks. exchangeToMono() or exchangeToFlux() can be used instead.

Thanks @rhubarb for the update.

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
  • 3
    Note that `exchange()` is [deprecated since 5.3](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/reactive/function/client/WebClient.RequestHeadersSpec.html#exchange--) in favour of `exchangeToMono()` or `exchangeToFlux()` – Rhubarb Jun 23 '21 at 10:52
19

According to spring Webclient api documentation the difference between the two is that exchange retrieve in addition to the body other http response information like headers and status, while retrieve only returns body information.

So If you only need the body information you should use retrieve, because it is a shortcut for exchange and then get the body, but if you need other information like http status you must use exchange.

alizeyn
  • 2,300
  • 1
  • 20
  • 30
JArgente
  • 2,239
  • 1
  • 9
  • 11
  • 4
    Important Note from the docs if anyone overlooked NOTE: Unlike retrieve(), when using exchange(), it is the responsibility of the application to consume any response content regardless of the scenario (success, error, unexpected data, etc). Not doing so can cause a memory leak. See ClientResponse for a list of all the available options for consuming the body. Generally prefer using retrieve() unless you have a good reason to use exchange() which does allow to check the response status and headers before deciding how or if to consume the response. – RRR_J Feb 04 '20 at 08:07
  • @RRR_J so basically if we use exchange() we need to write code to handle success, error, and any app specific data validation? Anything else? – heug Mar 10 '20 at 22:02
0

I think the use of retrieve() is more suitable and functional when you need just the body of Api because it's handling the status of your 4xx and 5xx range.