Spring-Web與Spring-WebFlux沖突問題解決
問題發(fā)現(xiàn)
創(chuàng)捷了Spring-Web項目,然后在學習Spring-WebFlux的時候代碼編寫后請求解決報404,示例代碼如下:
@Component
public class MyHandler {
public Mono<ServerResponse> handleRequest(ServerRequest request) {
// 處理請求邏輯
String name = request.queryParam("name").orElse("Anonymous");
String message = "Hello, " + name + "!";
// 構建響應
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>
啟動成功后,請求如圖

然后進行一系列的問題排查
問題解決
Spring MVC 和 Spring WebFlux 是兩個不同的框架,用于構建 Web 應用程序。由于這兩個框架之間的差異,建議一個項目只用一個框架。
請求404,先baidu,發(fā)現(xiàn)對這個問題解決方案特別少,然后看網(wǎng)上別人的案例發(fā)現(xiàn)都有@EnableWebFlux注解,加上后啟動一堆錯誤來了。
一切的來源都是Spring MVC 和Spring WebFlux一起使用導致的,所以還是重新建一個項目再學習把。
問題一: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重復了,但是全局搜索找不到這個bean,應該是默認自帶的。
再application.properties或application.yml文件中加入配置:
spring.main.allow-bean-definition-overriding=true
當您的應用程序啟動時,現(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注解,以為是沒加注解導致訪問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注解,刪除后,重啟即可(其實不加注解也是可以請求的)。
問題三:請求404
將上面的問題都解決后,請求發(fā)現(xiàn)還是報404,然后就是再pom依賴文件中,刪除spring-web依賴,重新編譯后重啟,發(fā)現(xiàn)依舊不行,最后發(fā)現(xiàn)SpringBoot可以知道應用程序的Web應用程序類型:
在配置文件(如application.properties或application.yml)中,添加以下屬性:
spring.main.web-application-type = reactive
它有兩個可選值:
SERVLET:表示將應用程序配置為使用傳統(tǒng)的Servlet API和阻塞I/O操作的Web堆棧。這是默認值,適用于大多數(shù)傳統(tǒng)的Spring MVC應用程序。
REACTIVE:表示將應用程序配置為使用Reactive編程模型和非阻塞I/O操作的Web堆棧。這適用于使用Spring WebFlux構建的響應式應用程序。
最后請求接口,如圖:

問題解決。
總結
到此這篇關于Spring-Web與Spring-WebFlux沖突問題解決的文章就介紹到這了,更多相關Spring-Web與Spring-WebFlux沖突內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)word轉pdf并在關鍵字位置插入圖片
這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)word轉pdf,并在word中關鍵字位置插入圖片,感興趣的小伙伴可以跟隨小編一起學習一下2024-11-11
SpringBoot統(tǒng)一響應和統(tǒng)一異常處理詳解
在開發(fā)Spring Boot應用時,處理響應結果和異常的方式對項目的可維護性、可擴展性和團隊協(xié)作有著至關重要的影響,統(tǒng)一結果返回和統(tǒng)一異常處理是提升項目質量的關鍵策略之一,所以本文給大家詳細介紹了SpringBoot統(tǒng)一響應和統(tǒng)一異常處理,需要的朋友可以參考下2024-08-08

