Hello, with my team we want to make a progressive ...
# spring
p
Hello, with my team we want to make a progressive transition to webflux. But we would like to use the coroutine support of webflux instead of Mono/Flux. I’ve seen in the presentation from Sébastien at KotlinConf that the support for coroutine in WebMVC controllers are not available yet. I’m wondering if it’s a good alternative to begin refactoring the app to have a big
runBlocking { }
inside the WebMVC controller until all of the controller are refactored, and when we are ready, to remove the dependency to webmvc to enable the full reactive stack, remove all the
runBlocking
and convert the controller methods to
suspend
?
s
Since
Deferred
should be supported via the Reactive adapter in Spring MVC, you could maybe wrap Coroutines code in
async { }
and just return the related
Deferred
return value. That's much better than
runBlocking { }
.
p
hum, does this mean that I should be theorically able to return a
Mono<>
from a WebMVC controller as well ? I should look into this “Reactive adapter”. Is this what’s described as HttpHandler in the doc ? https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-httphandler
because I tried to change one of my WebMVC controller to return a Mono<>. But the body of the response was empty, even though, with logging, I knew that the Mono was being subscribed to
s
Yes
Mono
should work as well
If you do simple tests with returning
Mono.just("foo)
does it work ?
p
Yes
I had a
OncePerRequestFilter
that was preventing the body to be returned, when i removed it, it works fine when returning a
Mono
.
But for coroutines, I can’t call
async
if I don’t have a scope. Is using
GlobalScope
inside a controller a good idea or should I stick with
runBlocking
?
s
Yes you can probably use
GlobalScope.async { }
p
👍
Now I just need to find out why my RequestFilter is breaking the Reactive adapter
a
If you check out mine and Jarle's talk from this years kotlinconf, you can see working examples of this, and pitfalls around it. If f.ex. you are using spring-security. runBlocking is a decent alternative in the itnerim, but ofc then everything is blocking, so it might not be worth it dependany on your use case.