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

ken*_*hui 3 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>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.删除spring-boot-starter-web依赖项时,它可以正常工作.

Bri*_*zel 5

正如关于WebFlux的Spring Boot参考文档部分所述,添加web和webflux启动器将配置Spring MVC Web应用程序.

这样做,因为许多现有的Spring Boot Web应用程序(使用MVC)将依赖于webflux启动程序来使用WebClient.Spring MVC部分支持反应式返回类型,因此这是一个预期的用例.相反的情况并非如此,因为反应式应用程序实际上不太可能使用Spring MVC位.

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

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)
Run Code Online (Sandbox Code Playgroud)

但是,清理依赖项仍然是一个好主意,因为在您的反应式Web应用程序中使用阻止功能很容易.