I am trying to use Spring Reactor with my Spring Boot application.
I am using Project Reactor 3.0.7.RELEASE and Spring Boot 1.5.3.RELEASE.
I have a method in my Service class which returns Flux.
I want to return the value to controller in web layer. But, I do not see the values returns in the json response.
When I invoke http://localhost:8080 from browser, I get response as, {"prefetch":-1}
I am not sure if I should I do some conversion from Flux to String before returning the response.
I have my code shared in , https://github.com/bsridhar123/spring-reactor-demo/blob/master/src/main/java/com/demo/reactor/ReactiveApp.java
Can you please help me on the correct approach for it.
To be eligible for a return and refund for all items, they must be unused and in the same condition that you received them, unless they are faulty and covered by our 60-day money back guarantee. The returned parcel is the responsibility of the customer until it has been received by FLUX Undies.
Use a simple domain model – Employee with an id and a name field. Build a REST API with a RestController to publish Employee resources as a single resource and as a collection. Build a client with WebClient to retrieve the same resource. Create a secured reactive endpoint using WebFlux and Spring Security.
What is Spring WebFlux ? Spring WebFlux is parallel version of Spring MVC and supports fully non-blocking reactive streams. It support the back pressure concept and uses Netty as inbuilt server to run reactive applications. If you are familiar with Spring MVC programming style, you can easily work on webflux also.
Blocking way to get value from Mono WebFlux. You can use the block() method to block the current thread indefinitely and wait for Mono to complete. If the Mono completes, then this method either returns the original value or null (Mono is empty). In case of any errors, then the exception is rethrown.
Full reactive support using Reactor is only implemented in Spring 5 (currently in RC phase) and Spring Boot 2 (currently in Milestone phase).
You can use Reactor independently of Spring, but that doesn't make the framework reactive and asynchronous so you lose some benefit. You cannot simply return a Flux
as the framework doesn't yet understand this type.
However, I believe it can still be useful in the service layer, if you have to orchestrate a lot of service calls.
What you can do in this case is to use Flux
and Mono
throughout your service layer and convert those to Spring 4's DeferredResult
. Something like:
DeferredResult<ResponseEntity<String>> result = new DeferredResult<>();
service.getSomeStringMono()
.map(n -> ResponseEntity.ok(n))
.defaultIfEmpty(ResponseEntity.notFound().build()
.subscribe(re -> result.setResult(re),
error -> result.setErrorResult(error));
return result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With