1
votes

spring-boot version : 2.2.6.RELEASE

kotlin version: 1.3.71

kotlin coroutine version: 1.3.5

I'm trying to used suspended function in a controller in spring boot. Here is the official doc that shows an example.

@RestController
@RequestMapping("/account/v1")
class UserAccountResourceV1 {
    @GetMapping("/username-taken", produces = [MediaType.TEXT_PLAIN_VALUE])
    suspend fun userNameTaken(): String {
        return "yes"
    }
}

I've added required dependencies in build.gradle.kts:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-jersey")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    implementation(group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version = "1.3.2")
}

It breaks in runtime throwing this err:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Unsupported suspending handler method detected: public java.lang.Object com.example.user.service.resources.UserAccountResourceV1.userNameTaken(kotlin.coroutines.Continuation)

complete stderr is here.

Need some help in figuring out what is wrong with my implementation.

1

1 Answers

2
votes

Figured out the solution.

implementation("org.springframework.boot:spring-boot-starter-web")

needed to be replaced with

implementation("org.springframework.boot:spring-boot-starter-webflux")

Basically spring-boot-starter-webflux brings support for suspened functions.