spring-boot-starter-web 和 spring-boot-starter-webflux 不能一起工作吗? [英] Don't spring-boot-starter-web and spring-boot-starter-webflux work together?

查看:63
本文介绍了spring-boot-starter-web 和 spring-boot-starter-webflux 不能一起工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始学习spring-webflux的时候,对这个组件有疑问.

我搭建了一个简单的项目,使用maven管理项目,并添加了spring-boot-starter-web和spring-boot-starter-webflux的依赖,如:

 <依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></依赖>

但它不起作用.去掉 spring-boot-starter-web 依赖后,可以正常工作.

解决方案

关于 WebFlux 的 Spring Boot 参考文档部分,添加 web 和 webflux 启动器将配置一个 Spring MVC web 应用程序.>

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

因此支持同时使用 web 和 webflux 启动器,但它会配置一个 Spring MVC 应用程序.您始终可以强制 Spring Boot 应用程序响应:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

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

When I start to learn the spring-webflux, I have the question about this component.

I builded the simple project, useing the maven to manage the project, and add the dependences about spring-boot-starter-web and spring-boot-starter-webflux", like :

    <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>

But it doesn't work. When removing the spring-boot-starter-web dependency, it can work well.

解决方案

As explained in the Spring Boot reference documentation section about WebFlux, adding both web and webflux starters will configure a Spring MVC web application.

This is behaving like that, because many existing Spring Boot web applications (using MVC) will depend on the webflux starter to use the WebClient. Spring MVC partially support reactive return types, so this is an expected use case. The opposite isn't really true, since a reactive application is not really likely to use Spring MVC bits.

So using both web and webflux starters is supported, but it will configure a Spring MVC application. You can always force the Spring Boot application to be reactive with:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

But it's still a good idea to clean up dependencies as it would be easy to use a blocking feature in your reactive web application.

这篇关于spring-boot-starter-web 和 spring-boot-starter-webflux 不能一起工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆