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

标签 java spring-boot spring-webflux

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

我构建了一个简单的项目,使用 maven 来管理它。我添加了与 spring-boot-starter-webspring-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 来使用 WebClientSpring 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/

相关文章:

java - Spring Boot - 无法解决 Whitelabel 错误页面

spring-boot - Spring WebFlux Route 总是返回 404

java - Facebook 点赞按钮的说明

java - 字典:硬编码与外部文件

java - ArithmeticException 偶尔被零除

java - Spring Security hasRole() 对于未经身份验证的用户,考虑角色层次结构

java - 字符串格式查询

java - 如何在端点函数体内检索请求方法?

java - 从 Flux 消费时按顺序调用非阻塞操作,包括重试

kotlin - Spring WebFlux,提取请求正文