Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot Webclient's retrieve vs exchange

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

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

Much Thanks.

like image 719
Abdullah Khan Avatar asked Oct 16 '19 09:10

Abdullah Khan


People also ask

What is the difference between rest template and Web client?

RestTemplate uses Java Servlet API and is therefore synchronous and blocking. Conversely, WebClient is asynchronous and will not block the executing thread while waiting for the response to come back. The notification will be produced only when the response is ready.

Is WebClient better than RestTemplate?

Compared to RestTemplate , WebClient has a more functional feel and is fully reactive. Since Spring 5.0, RestTemplate is deprecated. It will probably stay for some more time but will not have major new features added going forward in future releases. So it's not advised to use RestTemplate in new code.

What is bodyToMono and bodyToFlux?

If you are retrieving a single item, use bodyToMono. It emits 0-1 items. For multiple items, use bodyToFlux. It emits 0-N items.

What is exchangeToMono?

The exchangeToMono and exchangeToFlux methods allow access to the ClientResponse along with its status and headers: Mono<String> response = headersSpec. exchangeToMono(response -> { if (response. statusCode(). equals(HttpStatus.


2 Answers

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.

like image 82
JArgente Avatar answered Oct 07 '22 03:10

JArgente


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.

like image 35
Abdullah Khan Avatar answered Oct 07 '22 03:10

Abdullah Khan