Spring-Web與Spring-WebFlux沖突問題解決
問題發(fā)現(xiàn)
創(chuàng)捷了Spring-Web項(xiàng)目,然后在學(xué)習(xí)Spring-WebFlux的時候代碼編寫后請求解決報404,示例代碼如下:
@Component public class MyHandler { public Mono<ServerResponse> handleRequest(ServerRequest request) { // 處理請求邏輯 String name = request.queryParam("name").orElse("Anonymous"); String message = "Hello, " + name + "!"; // 構(gòu)建響應(yīng) return ServerResponse.ok().body(BodyInserters.fromValue(message)); } } @Configuration @EnableWebFlux public class MyWebFluxConfig { @Bean public RouterFunction<ServerResponse> route(MyHandler handler) { return RouterFunctions.route() .GET("/hello", handler::handleRequest) .build(); } }
pom依賴文件
<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>
啟動成功后,請求如圖
然后進(jìn)行一系列的問題排查
問題解決
Spring MVC 和 Spring WebFlux 是兩個不同的框架,用于構(gòu)建 Web 應(yīng)用程序。由于這兩個框架之間的差異,建議一個項(xiàng)目只用一個框架。
請求404,先baidu,發(fā)現(xiàn)對這個問題解決方案特別少,然后看網(wǎng)上別人的案例發(fā)現(xiàn)都有@EnableWebFlux
注解,加上后啟動一堆錯誤來了。
一切的來源都是Spring MVC 和Spring WebFlux一起使用導(dǎo)致的,所以還是重新建一個項(xiàng)目再學(xué)習(xí)把。
問題一:The bean ‘requestMappingHandlerMapping’, defined in class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class],
完整錯誤日志內(nèi)容:
The bean ‘requestMappingHandlerMapping’, defined in class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class] and overriding is disabled.
這個問題就是說requestMappingHandlerMapping的Bean重復(fù)了,但是全局搜索找不到這個bean,應(yīng)該是默認(rèn)自帶的。
再application.properties
或application.yml
文件中加入配置:
spring.main.allow-bean-definition-overriding=true
當(dāng)您的應(yīng)用程序啟動時,現(xiàn)有的bean定義將被新的定義所覆蓋。
問題二:The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via @EnableWebMvc and @EnableWebFlux, in the same application.
看網(wǎng)上案例都有加@EnableWebFlux
注解,以為是沒加注解導(dǎo)致訪問404,加上后報錯:
Caused by: java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via @EnableWebMvc and @EnableWebFlux, in the same application.
找到@EnableWebMvc
注解,刪除后,重啟即可(其實(shí)不加注解也是可以請求的)。
問題三:請求404
將上面的問題都解決后,請求發(fā)現(xiàn)還是報404,然后就是再pom依賴文件中,刪除spring-web依賴,重新編譯后重啟,發(fā)現(xiàn)依舊不行,最后發(fā)現(xiàn)SpringBoot可以知道應(yīng)用程序的Web應(yīng)用程序類型:
在配置文件(如application.properties
或application.yml
)中,添加以下屬性:
spring.main.web-application-type = reactive
它有兩個可選值:
SERVLET:表示將應(yīng)用程序配置為使用傳統(tǒng)的Servlet API和阻塞I/O操作的Web堆棧。這是默認(rèn)值,適用于大多數(shù)傳統(tǒng)的Spring MVC應(yīng)用程序。
REACTIVE:表示將應(yīng)用程序配置為使用Reactive編程模型和非阻塞I/O操作的Web堆棧。這適用于使用Spring WebFlux構(gòu)建的響應(yīng)式應(yīng)用程序。
最后請求接口,如圖:
問題解決。
總結(jié)
到此這篇關(guān)于Spring-Web與Spring-WebFlux沖突問題解決的文章就介紹到這了,更多相關(guān)Spring-Web與Spring-WebFlux沖突內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)word轉(zhuǎn)pdf并在關(guān)鍵字位置插入圖片
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)word轉(zhuǎn)pdf,并在word中關(guān)鍵字位置插入圖片,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11SpringBoot統(tǒng)一響應(yīng)和統(tǒng)一異常處理詳解
在開發(fā)Spring Boot應(yīng)用時,處理響應(yīng)結(jié)果和異常的方式對項(xiàng)目的可維護(hù)性、可擴(kuò)展性和團(tuán)隊協(xié)作有著至關(guān)重要的影響,統(tǒng)一結(jié)果返回和統(tǒng)一異常處理是提升項(xiàng)目質(zhì)量的關(guān)鍵策略之一,所以本文給大家詳細(xì)介紹了SpringBoot統(tǒng)一響應(yīng)和統(tǒng)一異常處理,需要的朋友可以參考下2024-08-08Java中使用注解校驗(yàn)手機(jī)號格式的詳細(xì)指南
在現(xiàn)代的Web應(yīng)用開發(fā)中,數(shù)據(jù)校驗(yàn)是一個非常重要的環(huán)節(jié),本文將詳細(xì)介紹如何在Java中使用注解對手機(jī)號格式進(jìn)行校驗(yàn),感興趣的小伙伴可以了解下2025-03-03