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

2022-09-02 09:45:15

当我开始学习 时,我有关于这个组件的问题。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


答案 1

有关 Web 环境的 Spring Boot 参考文档部分所述,同时添加 Web 和 webflux 启动器将配置 Spring MVC Web 应用程序。

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

因此,支持同时使用Web和webflux启动器,但它将配置Spring MVC应用程序。您始终可以强制 Spring Boot 应用程序对以下各项进行响应:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

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


答案 2

我在使用和导致时遇到了类似的问题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);
    }
}

整个班级看起来像这样

enter image description here

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

Web


推荐