带有@EnableWebFlux 注释的SpringWebFlux 错误

SpringWebFlux Error with @EnableWebFlux annotation

我正在使用 spring 启动 2.1.1 版本并使用 @EnableWebFlux 但我遇到了一些错误。

错误是

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.reactive.config.DelegatingWebFluxConfiguration': Initialization of bean failed; nested exception is java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via @EnableWebMvc and @EnableWebFlux, in the same application

我该如何解决这个问题。

您不能在 Spring SPR-16609

中同时启用 Spring MVC 和 Spring WebFlux

enabling MVC and WebFlux in the same application context which triggers a conflict

you can't have them in the same process currently.

它提供了使用反应性存储库的解决方法:

However, you can use reactive repositories from your existing Spring MVC application, and return the reactive types (Flux or Mono), from Spring MVC controller methods.

基于 Spring Docs and SpringFramework Collaborator Juergen Hoeller,您可以在同一应用中同时使用 Spring MVC 和 Spring WebFlux。通过这种方式,您默认使用 Spring MVC,而您可以同时启用响应式端点。请记住,它本质上是您正在使用的基于 servlet 的 MVC 应用程序。

如果除此之外还有 @EnableWebFlux,则意味着您希望严格地将其转换为响应式 Web 应用程序。

我的建议是关闭 @EnableWebFlux 并让您的应用程序直接基于 MVC。您仍然可以通过导入 Webflux 依赖项来使用一些响应式功能,例如 Mono<>Flux<>