Try to search your question here, if you can't find : Ask Any Question Now ?

How to properly drain / release response body from WebFlux Client?

HomeCategory: stackoverflowHow to properly drain / release response body from WebFlux Client?
pushpa asked 1 week ago

I am using WebFlux HTTP Client from Spring 5 reactive stack to access external REST service. I want to process responses based on HTTP status:

  1. If status is 2xx I want to return Mono with deserizlized response body.

  2. If status is 404 I want to drop response body and immediately return empty Mono.

  3. For any other status I want to drop response body and return error Mono with MyBusinessException.

My code looks like this:

webClient.get()
    .uri("/search")
    .syncBody(request)
    .exchange()
    .flatMap { response ->
        when {
            response.statusCode().is2xxSuccessful -> response.bodyToMono(MyResponse::class.java)
            response.statusCode() == NOT_FOUND -> Mono.empty()
            else -> MyBusinessException().toMono<MyResponse>()
        }
     }

I don’t want to waste time to receive and process response body when I don’t need it. JavaDoc for exchange() method states

You must always use one of the body or entity methods of the response to ensure resources are released.

How exactly should I do this if I want to drain response body and return result immediately?

1 Answers
Best Answer
Fernando answered 1 week ago
Your Answer

11 + 9 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com