I am trying out the support for suspend
handler functions as described in this part of the documentation. I've had great success in other projects using the coRouter
DSL but wanted to see how it looks and feels when using suspend
directly on handlers.
First of all I wanted to say that I found it a bit hard to understand in the linked documentation if suspend
handlers are only for Spring Webflux or if they will also work in Spring MVC (basically the spring-boot-starter-web
package).
Anyway, in neither case could I get them to work. I always get a NPE when the coroutine context is being accessed.
For Webflux:
2025-02-24T09:43:52.976+01:00 ERROR 97430 --- [ux-http-epoll-2] a.w.r.e.AbstractErrorWebExceptionHandler : [dc4ac96e-1] 500 Server Error for HTTP GET "/hello-suspend"
java.lang.NullPointerException: null
at kotlin.coroutines.jvm.internal.ContinuationImpl.getContext(ContinuationImpl.kt:105) ~[kotlin-stdlib-2.1.10.jar:2.1.10-release-473]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
*__checkpoint ⇢ HTTP GET "/hello-suspend" [ExceptionHandlingWebHandler]
Original Stack Trace:
at kotlin.coroutines.jvm.internal.ContinuationImpl.getContext(ContinuationImpl.kt:105) ~[kotlin-stdlib-2.1.10.jar:2.1.10-release-473]
at kotlin.coroutines.jvm.internal.ContinuationImpl.intercepted(ContinuationImpl.kt:112) ~[kotlin-stdlib-2.1.10.jar:2.1.10-release-473]
at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.intercepted(IntrinsicsJvm.kt:182) ~[kotlin-stdlib-2.1.10.jar:2.1.10-release-473]
at kotlinx.coroutines.DelayKt.delay(Delay.kt:172) ~[kotlinx-coroutines-core-jvm-1.8.1.jar:na]
at com.example.app.Controller.getHello(Controller.kt:14) ~[main/:na]
[... abbreviated]
at java.base/java.lang.Thread.run(Thread.java:1583) ~[na:na]
I hope I have not overlooked something simple. I tried to play around with dependencies and versions, but it never worked. I have attached a demo application with a test that fails.
Thank you for your support.
Comment From: FrontierPsychiatrist
I think attaching the demo failed, so doing that again: suspend-handler-failing.zip
Comment From: dmitrysulman
@FrontierPsychiatrist you're missing kotlin-reflect
on the classpath, this is mentioned in the Kotlin requirements.