【发布时间】:2020-08-06 10:53:36
【问题描述】:
spring-boot 版本:2.2.6.RELEASE
kotlin 版本:1.3.71
kotlin 协程版本:1.3.5
我正在尝试在 Spring Boot 的控制器中使用暂停功能。 Here 是显示示例的官方文档。
@RestController
@RequestMapping("/account/v1")
class UserAccountResourceV1 {
@GetMapping("/username-taken", produces = [MediaType.TEXT_PLAIN_VALUE])
suspend fun userNameTaken(): String {
return "yes"
}
}
我在 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")
}
它在运行时中断并抛出此错误:
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)
完整的标准错误是here。
需要一些帮助来弄清楚我的实现有什么问题。
【问题讨论】:
标签: spring-boot kotlin kotlin-coroutines