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

Don't spring-boot-starter-web and spring-boot-starter-webflux work together?

当我开始学习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 WebFlux 中所述,添加 web 和 webflux starter 将配置 Spring MVC web 应用程序。

这是这样的行为,因为许多现有的 Spring 引导 Web 应用程序(使用 MVC)将依赖于 webflux 启动器来使用 WebClient。 Spring MVC partially support reactive return types,所以这是一个预期的用例。相反的情况并非如此,因为反应式应用程序不太可能使用 Spring MVC 位。

因此支持同时使用 web 和 webflux 启动器,但它会配置一个 Spring MVC 应用程序。您始终可以强制 Spring 引导应用程序对以下内容作出反应:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

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

我在使用 spring-boot-starter-webfluxspring-data-geode 时遇到了类似的问题,导致

DEBUG [http-nio-8082-exec-2] org.sprin.web.servl.resou.ResourceHttpRequestHandler 454 handleRequest: Resource not found

已通过更改应用程序类型解决

@SpringBootApplication
public class Web {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Web.class);
        app.setWebApplicationType(WebApplicationType.REACTIVE);
        SpringApplication.run(Web.class, args);
    }
}

整个class长这样

设置应用程序类型后,如果我不以静态方式调用 SpringApplication,我会得到: