欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Spring-Web与Spring-WebFlux冲突问题解决,

来源: javaer 分享于  点击 1651 次 点评:24

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 MVC 和 Spring WebFlux 是两个不同的框架,用于构建 Web 应用程序。由于这两个框架之间的差异,建议一个项目只用一个框架。

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

    一切的来源都是Spring MVC 和Spring 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冲突问题解决的文章就介绍到这了,更多相关Spring-Web与Spring-WebFlux冲突内容请搜索3672js教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持3672js教程!

    您可能感兴趣的文章:
    • Spring Boot webflux使用方法解析
    • 如何在Spring WebFlux的任何地方获取Request对象
    • Spring WebFlux的使用指南
    • SpringBoot之webflux全面解析
    • SpringBoot深入分析webmvc和webflux的区别
    相关栏目:

    用户点评