当我开始学习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 starter 来使用 WebClient
。 Spring MVC partially support reactive return types ,所以这是一个预期的用例。反之则不然,因为响应式(Reactive)应用程序不太可能使用 Spring MVC 位。
因此支持同时使用 web 和 webflux 启动器,但它会配置一个 Spring MVC 应用程序。您始终可以强制 Spring Boot 应用程序响应:
SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)
但清除依赖项仍然是一个好主意,因为在响应式(Reactive) Web 应用程序中使用阻塞功能会很容易。
关于java - spring-boot-starter-web 和 spring-boot-starter-webflux 不能一起工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51377675/