java - spring-boot-starter-web 和 spring-boot-starter-webflux 不能一起工作吗?

标签 java spring-boot spring-webflux

当我开始学习spring-webflux时,我对这个组件有疑问。

我构建了一个简单的项目,使用maven来管理它。我添加了与 spring-boot-starter-web 和 spring-boot-starter-webflux 相关的依赖项,例如:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

但是这不起作用。当删除 spring-boot-starter-web 依赖时,它可以很好地工作。

最佳答案

the Spring Boot reference documentation section about the web environment 中所述,添加 web 和 webflux 启动器将配置 Spring MVC Web 应用程序。

这就是这样的,因为许多现有的 Spring Boot Web 应用程序(使用 MVC)将依赖于 webflux 启动器来使用 WebClientSpring MVC partially support reactive return types ,所以这是一个预期的用例。相反的情况则不然,因为响应式应用程序不太可能使用 Spring MVC 位。

因此支持使用 web 和 webflux 启动器,但它将配置 Spring MVC 应用程序。您始终可以强制 Spring Boot 应用程序使用react:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

但是清理依赖关系仍然是一个好主意,因为在响应式(Reactive) Web 应用程序中使用阻塞功能会很容易。

关于java - spring-boot-starter-web 和 spring-boot-starter-webflux 不能一起工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668246/

相关文章:

java - 带调试信息编译的 Java 程序不应该在生产系统中使用吗?

java - 在将输入字符串解析为 double 时保留其格式

spring-boot - Spring Cloud Stream & Kafka : Kafka Producer serialization error

java - Spring webflux 中的异常处理

java - 将 RxJava Single 转换为 Mono

java - 用户定义的检查异常处理

在jasper报告中以指定格式显示浮点值的java代码

java - 无法将java.util.Date值与MySQL datetime列值进行比较

java - BeanCurrentlyInCreationException Spring Boot

spring-webflux - 如何确定 Spring WebClient 是否使用 HTTP/2?