Quick revision
The short answer
retrieve() is the everyday path: it extracts the body for us and applies status handlers, so by default a 4xx or 5xx response throws an exception. exchange() hands us the raw request and response in a callback instead; no status handlers are applied, so we check the status ourselves and decode the body inside the callback, because the response is closed when the callback returns. We use exchange() for unusual flows, such as reading headers before deciding how to decode.
What to remember
In this answer, we saw that retrieve() is the common path in RestClient: it extracts the body with body() or toEntity() and applies status handlers, throwing HttpClientErrorException or HttpServerErrorException by default. exchange() gives full access to the request and response in a callback, applies no status handlers, and requires us to check the status and read the body inside the callback because the response is closed afterwards. We choose exchange() for unusual flows such as reading headers before deciding how to decode.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
retrieve() is the everyday path: it extracts the body for us and applies status handlers, so by default a 4xx or 5xx response throws an exception. exchange() hands us the raw request and response in a callback instead; no status handlers are applied, so we check the status ourselves and decode the body inside the callback, because the response is closed when the callback returns. We use exchange() for unusual flows, such as reading headers before deciding how to decode.
Why this question is asked
RestClient gives us two ways to finish a request, and the difference decides who is responsible for errors and for the response stream. Mixing them up leads to two classic bugs: code that silently treats a 500 as success, and code that tries to read a body after the response was closed. Interviewers ask this to see whether we know what each method does for us and what it leaves to us. In this answer, we will recap how a RestClient call is built, look at retrieve() and its default error handling, then at exchange() with an example where it is the right tool, and finish with a comparison and the rule for choosing.
1A Quick Recap of a RestClient Call
A RestClient call is a chain. We pick the method with get(), post() and so on, set the URI, headers and body, and then choose how to receive the response. That last step is where retrieve() and exchange() differ.
Until that step, nothing has been sent. With retrieve(), the request is actually executed when we call a terminal method such as body(), toEntity() or toBodilessEntity(). With exchange(), it is executed when exchange() is called, and our callback runs with the response.
2Retrieve(): Body Extraction With Status Handlers
retrieve() is designed for the normal case: we expect success and want the body as an object. It checks the status first. If we registered handlers with onStatus, it uses them; otherwise a default handler throws for every 4xx and 5xx.
Notice that the exceptions are typed: HttpClientErrorException for 4xx and HttpServerErrorException for 5xx, both subclasses of RestClientResponseException, which carries the status, headers and the error body as text. We can replace that default with our own rule per call, for example .onStatus(status -> status.value() == 404, (req, res) -> { throw new UserNotFound(42); }). toEntity() gives us the status and headers as well as the body, which covers many cases where people reach for exchange() unnecessarily.
3Exchange(): Full Control, Full Responsibility
exchange() takes a function that receives the request and the response, and whatever the function returns becomes the result of the call. No status handlers run. A 500 is just a response with status 500, and if we ignore the status, our code carries on as if it succeeded.
Two responsibilities move to us. The first is the status: every branch must be handled explicitly. The second is the response body. By default, RestClient closes the response as soon as our function returns, so the body must be read, or converted with bodyTo(), inside the callback. Returning an InputStream from the callback and reading it later fails because the stream is already closed. There is an overload, exchange(function, false), that leaves the response open, and then closing it is entirely our job; we use it only when we must stream a large body to somewhere else.
4When exchange() Is the Right Tool
exchange() is for flows where the decision about how to decode depends on the response itself. A common example is an API that returns either a normal body or a problem details document, and signals which one by its content type, sometimes even with status 200. Another is a download where we check headers such as Content-Length or ETag before deciding whether to read the body at all.
| Aspect | retrieve() | exchange() |
|---|---|---|
| Typical use | Expect success, want the body | Decide based on status or headers |
| Status handlers | Applied; default throws on 4xx and 5xx | Not applied; we check the status |
| Body decoding | body(), toEntity(), toBodilessEntity() | response.bodyTo() inside the callback |
| Response closing | Handled for us | Closed after the callback, unless close=false |
| Risk | Little | A forgotten status check hides errors |
With retrieve(), we would have to pick the target type before seeing the response. Here we read the header first and choose. That is the kind of case exchange() exists for; for a plain 'give me the object or throw', retrieve() is shorter and safer.
5Follow-up questions
How do I get headers and status with retrieve()?
Use toEntity(UserDto.class), which returns a ResponseEntity with status, headers and body, or toBodilessEntity() when there is no body. Error statuses still go through the status handlers, so toEntity() is usually all we need for success responses with interesting headers.
Can I set the same error rules for every call?
Yes. RestClient.Builder has defaultStatusHandler, which registers a status handler for every retrieve() on clients built from that builder. Per-call onStatus handlers are checked first, so individual calls can still override the default.
Is exchange() the same as RestTemplate's exchange()?
No, despite the name. RestTemplate.exchange() is a general method that still applies the error handler and returns a ResponseEntity. RestClient.exchange() is a low-level hook that gives us the raw response and applies no error handling.
6Conclusion
In this answer, we saw that retrieve() is the common path in RestClient: it extracts the body with body() or toEntity() and applies status handlers, throwing HttpClientErrorException or HttpServerErrorException by default. exchange() gives full access to the request and response in a callback, applies no status handlers, and requires us to check the status and read the body inside the callback because the response is closed afterwards. We choose exchange() for unusual flows such as reading headers before deciding how to decode.
Open with
retrieve() is what I use almost always. It gives me body() or toEntity() to extract the response, and it applies status handlers: either the ones I register with onStatus, or a default that throws HttpClientErrorException for 4xx and HttpServerErrorException for 5xx.
Then, if they want more
So errors cannot slip through silently, and the response is closed for me. exchange() is the low-level option. I pass a function that gets the raw request and response, and whatever it returns is the result. No status handlers run, so I have to check the status myself; if I forget, a 500 looks like a success.
And I have to read the body inside the callback, using response.bodyTo, because RestClient closes the response when the callback returns. There is an overload that keeps it open, but then closing it is my responsibility.
I reach for exchange() when how I decode depends on the response, for example when an API signals a problem details body only through the content type, or when I want to look at headers like ETag or Content-Length before reading the body. If I just need headers and status on success, toEntity() with retrieve() is enough.
And I would not confuse it with RestTemplate's exchange method, which still applies the error handler.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the difference between retrieve() and exchange() in RestClient?": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/36cc6c35f0/