5
votes

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.

1
That isn't supported you need Spring Boot 2.0 and Spring 5 to be able to work with Reactive.M. Deinum
Spring 5 is expected only June End. Spring Boot 2.0 GA is also expected only end of year. But, Spring Reactor Core is already in GA Release for a long time. What is the use of Spring Reactor ? Can't we use Spring Reactor in current release of Spring 1.5.3 itself. Should we hold on our dev/production till end of year.?Jbaur
There is no such thing as Spring Reactor the project is called Reactor and has no relation to Spring (other then both are maintained by Pivotal). You can use Reactive but not in the web layer as that will only be added in Spring 5 and not back ported (the main theme is JDK8 and Reactive) the other projects (Boot, Data, Integration) follow this and the next major releases will natively support Reactive programming.M. Deinum
I am referring to projectreactor.io/docs/core/release/reference/docs/index.html. Can't we use this with Spring Boot?Jbaur
Trust me I know what you are referring to. As stated yes you can use it but there is no native support. Everything you need you will need to do yourself. No reactive Spring Data repositories, no reactive web components etc. etc. all that is available with after the release of Spring 5.M. Deinum

1 Answers

5
votes

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;