关于java: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-web和spring-boot-starter-webflux的依赖项,例如:

1
2
3
4
5
6
7
8
9
    <dependency>
        <groupId>org.springframework.boot</groupId>
        spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        spring-boot-starter-web</artifactId>
    </dependency>

但这是行不通的。 当删除spring-boot-starter-web依赖项时,它可以很好地工作。


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

之所以这样,是因为许多现有的Spring Boot Web应用程序(使用MVC)将依赖webflux启动器来使用WebClient。 Spring MVC部分支持响应式返回类型,因此这是一个预期的用例。相反的情况并非如此,因为反应性应用程序不太可能使用Spring MVC位。

因此,支持同时使用web和webflux启动器,但它将配置Spring MVC应用程序。您可以始终通过以下方式强制Spring Boot应用程序进行反应:

1
SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

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


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

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

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

1
2
3
4
5
6
7
8
@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