Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Return Flux as response when using Spring Reactor and Spring Boot?

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.

like image 418
Jbaur Avatar asked May 31 '17 09:05

Jbaur


People also ask

How do I return flux?

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.

How do you use spring flux?

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.

How does spring WebFlux work internally?

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.

How do I get value from mono WebFlux?

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.


1 Answers

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;
like image 92
Simon Baslé Avatar answered Nov 01 '22 13:11

Simon Baslé