Skip to content
Advertisement

Spring WebFlux – Why I have to wait for WebClient response?

I have a my WebClient class as per below:

JavaScript

After execution I get this on my console:

JavaScript

Question: If I comment Thread.sleep(1000); then I dont get any response. Why do I need to wait for the response?

Advertisement

Answer

Your code is working with Thread.sleep(1000); because you are blocking the parent thread for some time and within this time you are getting a response back from WebClient.

WebClient is a non-blocking HTTP client. As you need to return the response back from the mySimpleTestMethod method, you need to block until you retrieve the response using Mono#block().

JavaScript

and then you can return the response back.

Also, note that in the below code you are calling toString on Disposable type (LambdaMonoSubscriber), LambdaMonoSubscriber doesn’t override toString method, as a result, you are getting string value (reactor.core.publisher.LambdaMonoSubscriber@60b71e8f) from toString method of the Object class.

JavaScript
Advertisement