简体   繁体   English

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

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

When I start to learn the spring-webflux, I have the question about this component.开始学习spring-webflux的时候,对这个组件有疑问。

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 :我搭建了一个简单的项目,使用maven管理项目,并添加spring-boot-starter-web和spring-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>

But it doesn't work.但它不起作用。 When removing the spring-boot-starter-web dependency, it can work well.删除 spring-boot-starter-web 依赖项时,它可以很好地工作。

As explained in the Spring Boot reference documentation section about WebFlux , adding both web and webflux starters will configure a Spring MVC web application.正如有关 WebFlux 的 Spring Boot 参考文档部分所述,添加 web 和 webflux 启动器将配置 Spring MVC Web 应用程序。

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 Boot Web 应用程序(使用 MVC)将依赖 webflux starter 来使用 WebClient。 Spring MVC partially support reactive return types , so this is an expected use case. Spring MVC 部分支持响应式返回类型,因此这是一个预期的用例。 The opposite isn't really true, since a reactive application is not really likely to use Spring MVC bits.相反的情况并非如此,因为响应式应用程序不太可能使用 Spring MVC 位。

So using both web and webflux starters is supported, but it will configure a Spring MVC application.因此支持同时使用 web 和 webflux starters,但它会配置一个 Spring MVC 应用程序。 You can always force the Spring Boot application to be reactive with:您始终可以强制 Spring Boot 应用程序响应:

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.但是清理依赖项仍然是一个好主意,因为在您的响应式 Web 应用程序中使用阻塞功能很容易。

I had a similar issue using spring-boot-starter-webflux and spring-data-geode causing我在使用spring-boot-starter-webfluxspring-data-geode遇到了类似的问题,导致

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

It was resolved by changing the application type已通过更改应用程序类型解决

@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);
    }
}

The whole class looks like this整个班级是这样的

在此处输入图片说明

After setting the application type, if I don't then call the SpringApplication in a static way, I get this:设置应用程序类型后,如果我不以静态方式调用SpringApplicationSpringApplication得到以下信息:

网络

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gradle 构建的 spring-boot-starter-webflux 依赖问题 - spring-boot-starter-webflux dependency issue with gradle build WebClient (spring-boot-starter-webflux) 向不正确的 url 发送请求 - WebClient (spring-boot-starter-webflux) sends request to incorrect url spring-boot-starter-webflux和spring-boot-starter-jetty之间的冲突 - Conflict between spring-boot-starter-webflux and spring-boot-starter-jetty 在Maven中看不到spring-boot-starter-data-mongodb-active和spring-boot-starter-webflux依赖性 - Dont see spring-boot-starter-data-mongodb-reactive and spring-boot-starter-webflux dependency in maven spring-boot-starter-web-reactive + spring-boot-starter-actuator不能同时工作? - spring-boot-starter-web-reactive + spring-boot-starter-actuator doesn't work together? 在没有 spring-boot-starter-web 的情况下配置 Spring Security AuthenticationManager - Configure Spring Security AuthenticationManager without spring-boot-starter-web spring-boot-starter-web和spring-boot-starter-web-services以及spring-boot-starter-jersey之间的区别 - Difference between spring-boot-starter-web and spring-boot-starter-web-services and spring-boot-starter-jersey 结合 ObjectDB 使用 Spring-Boot-Starter-Web - Using Spring-Boot-Starter-Web in combination with ObjectDB 使用 spring-boot-starter-web “找不到可接受的表示” - "Could not find acceptable representation" using spring-boot-starter-web 如何防止嵌入式netty服务器从spring-boot-starter-webflux开始? - How to prevent embedded netty server from starting with spring-boot-starter-webflux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM