简体   繁体   English

Spring WebFlux Reactive 和 Kotlin 协程启动错误

[英]Spring WebFlux Reactive and Kotlin Coroutines Startup Error

No matter how I set my project up I get the following exception on startup:无论我如何设置我的项目,我都会在启动时遇到以下异常:

"Unsupported suspending handler method detected". “检测到不支持的挂起处理程序方法”。

I'm trying to use the support for coroutines described in https://docs.spring.io/spring/docs/5.2.0.BUILD-SNAPSHOT/spring-framework-reference/languages.html#coroutines .我正在尝试使用对https://docs.spring.io/spring/docs/5.2.0.BUILD-SNAPSHOT/spring-framework-reference/languages.html#coroutines中描述的协程的支持。

Here's my gradle setup up (abbreviated).这是我的 gradle 设置(缩写)。 How do I get rid of this exception?我如何摆脱这个异常?

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"

This probably happens because when there are both Spring MVC and Spring WebFlux in the classpath, Spring MVC server is used by Spring Boot because Spring WebFlux also contains the WebClient that can be used with both servers. This probably happens because when there are both Spring MVC and Spring WebFlux in the classpath, Spring MVC server is used by Spring Boot because Spring WebFlux also contains the WebClient that can be used with both servers.

It usually happens when you are using spring-boot-starter-webflux with a dependency (here springfox-swagger2 ) that brings transitively Spring MVC in the classpath.当您使用带有依赖项(此处springfox-swagger2spring-boot-starter-webflux时,通常会发生这种情况,该依赖项会在类路径中传递 Spring MVC。

Setting spring.main.web-application-type=reactive in your application.properties (or the equivalent for application.yml ) will force using WebFlux server and probably avoid this issue.application.properties (或application.yml的等效项)中设置spring.main.web-application-type=reactive将强制使用 WebFlux 服务器并可能避免此问题。

Notice that Spring MVC supports Coroutines as well (using internally MVC async support) as of Spring Framework 5.3 and Spring Boot 2.4 so with those versions you will not have this error, but you will have Spring MVC used instead of Spring WebFlux which is probably not what you want. Notice that Spring MVC supports Coroutines as well (using internally MVC async support) as of Spring Framework 5.3 and Spring Boot 2.4 so with those versions you will not have this error, but you will have Spring MVC used instead of Spring WebFlux which is probably not你想要什么。 So even with Spring Boot 2.4+ you should set spring.main.web-application-type=reactive if you want to use Spring WebFlux with a dependency that pulls transitively Spring MVC. So even with Spring Boot 2.4+ you should set spring.main.web-application-type=reactive if you want to use Spring WebFlux with a dependency that pulls transitively Spring MVC.

I had the same issue for a few hours here.我在这里有几个小时同样的问题。

The way I solved this was by not using the @RequestMapping annotation.我解决这个问题的方法是不使用 @RequestMapping 注释。

Removing that annotation and moving the @RequestMapping values down the the @GetMapping and @PostMappings, etc worked.删除该注释并将@RequestMapping 值向下移动到@GetMapping 和@PostMappings 等工作。 Not a good solution by any means, but the only one I've found so far.无论如何都不是一个好的解决方案,但到目前为止我发现的唯一一个。

It seems like the problem is coming from Spring MVC adding a check in the code for suspending funtions, which is shared with WebFlux I guess: https://github.com/spring-projects/spring-framework/issues/23585似乎问题来自 Spring MVC,在代码中添加了暂停功能的检查,我猜这是与 WebFlux 共享的: https://github.com/spring-projects/spring-framework/issues/23585

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM