How do I return Flux as a 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 that returns Flux.

I want to return a value to a controller at the web tier. But I don't see the values ​​being returned in the json response.

When I call http: // localhost: 8080 from the browser, I get a response like, {"Proactive": - 1}

I'm not sure if I should do some conversion from Flux to String before returning the response.

I have my code, https://github.com/bsridhar123/spring-reactor-demo/blob/master/src/main/java/com/demo/reactor/ReactiveApp.java

Could you please help me on the right approach to this.

+3


source to share


1 answer


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 Flux

as the struct has not yet understood the type.

However, I believe it can still be useful at the service level if you need to organize a lot of service calls.



What you can do in this case is to use Flux

both Mono

the entire service layer and convert them to Spring 4 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;

      

+3


source







All Articles