当我开始学习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 启动器来使用 WebClient
。 Spring 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/