konrad-kaminski / spring-kotlin-coroutine

Kotlin coroutine support for Spring.
448 stars 69 forks source link

Getting NullPointerException Question #32

Closed mitrejcevski closed 5 years ago

mitrejcevski commented 5 years ago

I'm trying to set up a basic spring-boot project and I have the following setup:

kotlinVersion = '1.2.70'
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")

I use the following dependencies:

 compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.26.1"
 compile 'org.springframework.kotlin:spring-kotlin-coroutine:0.3.6'

And here is what I have:

@SpringBootApplication
@EnableCoroutine
class SimpleApplication

fun main(args: Array<String>) {
    runApplication<SimpleApplication>(*args)
}

@RestController
class ExampleController {

    @GetMapping("sample")
    suspend fun sample(): Int {
        log.info("In sample")
        delay(1000)
        log.info("In simple - after delay")
        return 1234
    }
}

When I hit the endpoint I get the following exception:

kotlin.KotlinNullPointerException: null
    at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.getFacade(CoroutineImpl.kt:36) ~[kotlin-stdlib-1.2.70.jar:1.2.70-release-54 (1.2.70)]
    at kotlin.coroutines.experimental.jvm.internal.CoroutineIntrinsics.normalizeContinuation(CoroutineIntrinsics.kt:18) ~[kotlin-stdlib-1.2.70.jar:1.2.70-release-54 (1.2.70)]
    at kotlinx.coroutines.experimental.DelayKt.delay(Delay.kt:102) ~[kotlinx-coroutines-core-0.26.1.jar:na]
    at kotlinx.coroutines.experimental.DelayKt.delay(Delay.kt:72) ~[kotlinx-coroutines-core-0.26.1.jar:na]
    at com.wetransfer.elephant.ExampleController.sample$suspendImpl(ElephantApplication.kt:25) ~[classes/:na]
    at com.wetransfer.elephant.ExampleController.sample(ElephantApplication.kt) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
....

When I comment out the delay(1000) line it returns the result nicely.

I would like to ask - am I doing something wrong?

Thanks!

RLejolivet commented 5 years ago

org.springframework.kotlin:spring-kotlin-coroutine doesn't support web methods by itself.

You need to add spring-webmvc-kotlin-coroutine or spring-webflux-kotlin-coroutine, depending on which spring web framework you're using, as a dependency. Then you'll be able to have suspending functions as web methods (such as your @GetMapping("sample") suspend fun sample() here)

konrad-kaminski commented 5 years ago

You're right. In fact this is actually stated in the documentation - see the Project modules section.