【汇总】解决Spring-Web与Spring-WebFlux冲突

作者 : admin 本文共2760个字,预计阅读时间需要7分钟 发布时间: 2024-06-10 共2人阅读

【汇总】解决Spring-Web与Spring-WebFlux冲突

  • 问题发现
  • 问题解决
    • 问题一:The bean ‘requestMappingHandlerMapping’, defined in class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class],
    • 问题二:The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via @EnableWebMvc and @EnableWebFlux, in the same application.
    • 问题三:请求404

问题发现

创捷了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-Web与Spring-WebFlux冲突插图

然后进行一系列的问题排查

问题解决

Spring MVCSpring WebFlux 是两个不同的框架,用于构建 Web 应用程序。由于这两个框架之间的差异,建议一个项目只用一个框架。

请求404,先baidu,发现对这个问题解决方案特别少,然后看网上别人的案例发现都有@EnableWebFlux注解,加上后启动一堆错误来了。

一切的来源都是Spring MVCSpring WebFlux一起使用导致的,所以还是重新建一个项目再学习把。

问题一:The bean ‘requestMappingHandlerMapping’, defined in class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class],

完整错误日志内容:

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.

这个问题就是说requestMappingHandlerMappingBean重复了,但是全局搜索找不到这个bean,应该是默认自带的。

application.propertiesapplication.yml文件中加入配置:

spring.main.allow-bean-definition-overriding=true

当您的应用程序启动时,现有的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.

看网上案例都有加@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

将上面的问题都解决后,请求发现还是报404,然后就是再pom依赖文件中,删除spring-web依赖,重新编译后重启,发现依旧不行,最后发现SpringBoot可以知道应用程序的Web应用程序类型:

在配置文件(如application.propertiesapplication.yml)中,添加以下属性:

spring.main.web-application-type = reactive

它有两个可选值:

  • SERVLET:表示将应用程序配置为使用传统的Servlet API和阻塞I/O操作的Web堆栈。这是默认值,适用于大多数传统的Spring MVC应用程序。

  • REACTIVE:表示将应用程序配置为使用Reactive编程模型和非阻塞I/O操作的Web堆栈。这适用于使用Spring WebFlux构建的响应式应用程序。

最后请求接口,如图:

【汇总】解决Spring-Web与Spring-WebFlux冲突插图(1)
问题解决。

本站无任何商业行为
个人在线分享 » 【汇总】解决Spring-Web与Spring-WebFlux冲突
E-->