Spring boot Webclient's retrieve vs exchange

Abdullah Khan picture Abdullah Khan · Oct 16, 2019 · Viewed 8.3k times · Source

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.

Answer

JArgente picture JArgente · Oct 22, 2019

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.