簡體   English   中英

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的依賴,如:

    <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 依賴項時,它可以很好地工作。

正如有關 WebFlux 的 Spring Boot 參考文檔部分所述,添加 web 和 webflux 啟動器將配置 Spring MVC Web 應用程序。

這就是這種行為,因為許多現有的 Spring Boot Web 應用程序(使用 MVC)將依賴 webflux starter 來使用 WebClient。 Spring MVC 部分支持響應式返回類型,因此這是一個預期的用例。 相反的情況並非如此,因為響應式應用程序不太可能使用 Spring MVC 位。

因此支持同時使用 web 和 webflux starters,但它會配置一個 Spring MVC 應用程序。 您始終可以強制 Spring Boot 應用程序響應:

SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)

但是清理依賴項仍然是一個好主意,因為在您的響應式 Web 應用程序中使用阻塞功能很容易。

我在使用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);
    }
}

整個班級是這樣的

在此處輸入圖片說明

設置應用程序類型后,如果我不以靜態方式調用SpringApplicationSpringApplication得到以下信息:

網絡

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM