How to extract response header status code from Spring 5 WebClient ClientResponse
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
With Spring 5 WebClient, you can extract the HTTP status code and headers either by working with ClientResponse directly or by letting Spring wrap the result in a ResponseEntity. The right choice depends on whether you need low-level control before reading the body.
If you only need body plus metadata, toEntity(...) is often the simplest route. If you need to inspect ClientResponse first and decide what to do based on status or headers, use exchangeToMono.
Use exchangeToMono When You Need ClientResponse
exchangeToMono gives you direct access to the ClientResponse, including status code and headers.
This is the most flexible option because you can branch on the status before deciding how to read the body.
Access Specific Headers
Headers are available through response.headers().asHttpHeaders().
The important point is to read the metadata while you still have the ClientResponse in hand.
Use toEntity(...) for the Easiest Body Plus Metadata Result
If you do not need custom branching on the raw response, retrieve().toEntity(...) is usually simpler.
This is often the cleanest answer when your real goal is simply "give me status, headers, and body together."
retrieve() Versus exchangeToMono
The difference is mostly about control.
- '
retrieve()is higher-level and convenient.' - '
exchangeToMonoexposes the rawClientResponse.'
If you use retrieve(), 4xx and 5xx responses are usually turned into errors automatically unless you customize that behavior. With exchangeToMono, you are responsible for inspecting the status and deciding what to do.
Blocking Example for Non-Reactive Code
If you are in a non-reactive context, you can still block for the result.
This is fine in tests or imperative application code. It is not appropriate inside an already reactive WebFlux request pipeline.
Pick the API That Matches the Return Shape
If your method should return only a deserialized body, retrieve() keeps the code compact. If your method must preserve transport metadata such as status, caching headers, or rate-limit headers, make that explicit in your design by returning ClientResponse-derived data or a ResponseEntity.
Common Pitfalls
- Using
retrieve()when you really needed low-level access toClientResponsebefore body extraction. - Forgetting that
retrieve()treats error status codes differently fromexchangeToMono. - Calling
.block()inside reactive request handling and then wondering why the pipeline stalls or fails. - Extracting only the body and then realizing later that you also needed headers or status metadata.
- Ignoring the response body entirely when working with
ClientResponse, which can lead to poor resource handling patterns.
Summary
- Use
exchangeToMonowhen you need direct access toClientResponse, including status and headers. - Use
retrieve().toEntity(...)when you want the simplest way to get body plus response metadata together. - Read headers through
response.headers().asHttpHeaders()orResponseEntity.getHeaders(). - Be explicit about how you want to handle non-2xx responses.
- Avoid
.block()inside reactive request pipelines.
Related reading
- How to extract value from JSON response when using Spring MockMVC
- How to fetch FetchType.LAZY associations with JPA and Hibernate in a Spring Controller
- How to find files that match a wildcard string in Java?
- How to find out the currently logged-in user in Spring Boot?
- How to find the index of an element in a TreeSet?
- How to find unused/dead code in java projects
- How to find/remove unused dependencies in Gradle
- How to fix Consider defining a bean of type 'org.jooq.DSLContext' in your configuration. after update to jOOQ 3.15.0

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.