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

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

问题描述

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

我构建了一个简单的项目,使用Maven来管理该项目,并添加了有关spring-boot-starter-web和spring-boot-starter-webflux的依赖项,例如:

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

但是它不起作用.删除spring-boot-starter-web依赖项时,它可以很好地工作.

解决方案

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天全站免登陆