Spring WebFlux Reactive 和 Kotlin Coroutines 启动错误

Mic*_*dge 2 exception coroutine kotlin spring-boot spring-webflux

无论我如何设置我的项目,我都会在启动时遇到以下异常:

“检测到不支持的挂起处理程序方法”。

我正在尝试使用https://docs.spring.io/spring/docs/5.2.0.BUILD-SNAPSHOT/spring-framework-reference/languages.html#coroutines 中描述的对协程的支持。

这是我的 gradle 设置(缩写)。我如何摆脱这个异常?

ext.kotlin_version = '1.3.70'
ext.kotlin_coroutines_core = '1.3.5'
ext.kotlin_coroutines_reactor = '1.3.5'
ext.spring_boot_version = '2.2.6.RELEASE'
ext.springfox_version='2.9.2'
ext.jackson_module_kotlin = '2.10.3'
...

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_core"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-reactor:$kotlin_coroutines_reactor"
implementation "org.springframework.boot:spring-boot-starter-webflux:$spring_boot_version"
implementation "com.fasterxml.jackson.module:jackson-module-kotlin:$jackson_module_kotlin"
implementation "net.rakugakibox.spring.boot:logback-access-spring-boot-starter:2.7.1"
implementation "net.logstash.logback:logstash-logback-encoder:5.3"
implementation "org.springframework.boot:spring-boot-starter-actuator:$spring_boot_version"
implementation "io.micrometer:micrometer-registry-statsd:1.1.4"
implementation "io.springfox:springfox-swagger2:$springfox_version"
Run Code Online (Sandbox Code Playgroud)

Séb*_*uze 8

这可能是因为当类路径中同时存在 Spring MVC 和 Spring WebFlux 时,Spring Boot 会使用 Spring MVC 服务器,因为 Spring WebFlux 还包含WebClient可以与这两个服务器一起使用的 。

当您使用spring-boot-starter-webflux依赖项(此处springfox-swagger2)在类路径中引入可传递的 Spring MVC时,通常会发生这种情况。

设置spring.main.web-application-type=reactive在您application.properties(或等值application.yml)将强制使用WebFlux服务器,可能避免此问题。

请注意,从 Spring Framework 5.3 和 Spring Boot 2.4 开始,Spring MVC 也支持协程(使用内部 MVC 异步支持),因此对于这些版本,您将不会出现此错误,但您将使用 Spring MVC 而不是 Spring WebFlux,这可能不是你想要什么。因此,即使使用 Spring Boot 2.4+,您也应该设置spring.main.web-application-type=reactive是否要使用具有传递性 Spring MVC 依赖项的 Spring WebFlux。