【问题标题】:Error creating bean with name 'requestMappingHandlerMapping' with kotlin suspend function in spring-boot在 spring-boot 中使用 kotlin 挂起函数创建名称为“requestMappingHandlerMapping”的 bean 时出错
【发布时间】: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


    【解决方案1】:

    想出了解决办法。

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

    需要替换为

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

    基本上spring-boot-starter-webflux 带来了对暂停功能的支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-26
      • 2019-02-27
      • 2021-01-21
      • 1970-01-01
      • 2016-06-21
      • 2020-07-27
      • 2017-04-17
      • 2019-04-27
      相关资源
      最近更新 更多