Ash
Ash

Reputation: 261

Webclient 200 status code with failure in responseBody

I am using webClient to make call to services 1 that call services 2 and then services 2 fetches data from database and returns them. Problem: I have onStatus to catch bad responses, so responses with 400 or 500 status codes. Code Example:

     return webClient.post()
                     .uri('/example')
                     .header('someHeader')
                     .body(PojoBody)
                     .retrieve()
                     .onStatus(HttpStatus:isError, handleError)
                     .bodyToMono(ResponsePojo);

In some edge cases, response is coming with 200 status code and response body has failure message and reason for failure. So how can I retrieve that and then throw an exception with that failure message (in response object returned by other services). Since it tries to map and throws error. Currently I am getting 500 internal server error. Any help will be greatly appreciated thanks.

Upvotes: 1

Views: 3738

Answers (2)

Carlos Zambrano
Carlos Zambrano

Reputation: 11

It may be too late to respond but the error is due to the maximum limit that WebClient can buffer, to solve this you can change the buffering size as follows:

final size = 10*1024*1024 // 10MB
final ExchangeStrategies strategies = ExchangeStrategies.builder()
.codecs(codecs -> codecs.defaultCodecs().maxInMemory(size)).build();

WebClient.Bulder builder = WebClient.builder()
.exchangeStrategies(strategies)
.baseUrl(url));

This is a small example

Upvotes: 1

lkatiforis
lkatiforis

Reputation: 6255

The onStatus method lets you handle specific HTTP status responses. You can use it for successful responses as well:

  .onStatus(HttpStatus::is2xxSuccessful, this::handleSuccess)
  .onStatus(HttpStatus::isError, this::handleError)  

Upvotes: 1

Related Questions