【问题标题】:SpringWebFlux Error with @EnableWebFlux annotation带有 @EnableWebFlux 注释的 SpringWebFlux 错误
【发布时间】:2019-06-01 20:03:59
【问题描述】:

我正在使用 spring boot 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

我该如何解决这个问题。

【问题讨论】:

  • 为什么要手动启用 Web Flux?如果那在您的类路径上,Spring Boot 将自动启用它。这同样适用于 web mvc 部分。看起来您的类路径触发器上的 spring-webmvcspring-webflux jar 都需要配置。
  • 好的。但是如果我想配置 WebFlux ,我该怎么做。通过启用 @EnableWebFlux 并从 WebFluxConfigurer.class 扩展?
  • 你不需要@EnableWebFlux,因为 Spring Boot 会自动为你做这件事。如果您想自定义配置,只需在您的项目中创建一个WebFluxConfigurer 并添加一个@Configuration 注释即可。
  • 啊哈明白了,非常感谢
  • 并且还要确保你的类路径中没有 web 和 WebFlux jar,否则 Spring Boot 将尝试同时配置这两者。正如您已经注意到的那样,这是不允许的。

标签: java spring-boot spring-mvc spring-webflux


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您不能在 Spring SPR-16609 中同时启用 Spring MVC 和 Spring WebFlux

    在触发冲突的同一应用程序上下文中启用 MVC 和 WebFlux

    目前你不能让它们在同一个进程中。

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

    但是,您可以使用现有 Spring MVC 应用程序中的响应式存储库,并从 Spring MVC 控制器方法返回响应式类型(Flux 或 Mono)。

    【讨论】:

    • 我没有使用 SpringMvc 。
    • org.springframework.bootspring-boot-starter-webflux
    • @NasibullohYandashev 所以从你的代码中删除@EnableWebFlux
    • @NasibullohYandashev 如果答案对您有帮助,您可以点击答案左侧的 V 符号接受它
    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多