Handling Responses
About
Goals of Response Handling
Available Handling Methods
Response Body Handling
1. bodyToMono(Class<T>) – For Single Object Responses
bodyToMono(Class<T>) – For Single Object Responses2. .bodyToFlux(Class<T>) – For List/Streaming Responses
.bodyToFlux(Class<T>) – For List/Streaming Responses3. .toEntity(Class<T>) – For Object + Metadata
.toEntity(Class<T>) – For Object + Metadata4. .toEntityList(Class<T>) – For List + Metadata
.toEntityList(Class<T>) – For List + Metadata5. .toBodilessEntity() – For Responses with No Body
.toBodilessEntity() – For Responses with No Body6. .exchangeToMono(response -> …) – Full Manual Handling
.exchangeToMono(response -> …) – Full Manual Handling7. .bodyToMono(new ParameterizedTypeReference<T>())
.bodyToMono(new ParameterizedTypeReference<T>())8. .exchangeToFlux(...)
.exchangeToFlux(...)9. .exchange(...) (Deprecated)
.exchange(...) (Deprecated)Converting Response to ResponseEntity
Syntax
Example 1
Example 2
Example 3 – Using with Exchange Filter Logging
Extracting Headers from Response
Approach 1: Extract Headers via toEntity(...)
toEntity(...)Approach 2: Using exchangeToMono(...) for More Control
exchangeToMono(...) for More ControlApproach 3: Inside a doOnNext() for Post-processing
doOnNext() for Post-processingHandling Multiple Values of a Header
Mapping Error Responses (Graceful Fallback)
Approach 1: Use .onStatus(...) for HTTP Status Handling
.onStatus(...) for HTTP Status HandlingExample: Return Default Response for 404
Approach 2: Handle Exceptions Like Timeout, IO, etc.
Example: Gracefully Handle Any Runtime Exception
Approach 3: Using exchangeToMono for Custom Fallback Based on Status + Body
exchangeToMono for Custom Fallback Based on Status + BodyApproach 4: Use Case in Fallback-Aware Services (e.g., Circuit Breaker)
Deserialize into Custom Error Object
1. Define Custom Error Class
2. Use onStatus with .bodyToMono(ApiError.class)
onStatus with .bodyToMono(ApiError.class)Last updated