当前位置: 首页 > news >正文

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

【汇总】解决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 {@Beanpublic 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 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构建的响应式应用程序。

最后请求接口,如图:

在这里插入图片描述
问题解决。

http://www.lryc.cn/news/288875.html

相关文章:

  • maven 依赖配置补充
  • Pandas ------ 向 Excel 文件中写入含有合并表头的数据
  • kafka summary
  • 【新书推荐】2.6节 原码、反码和补码
  • docker 网络及如何资源(CPU/内存/磁盘)控制
  • 安装 nvm
  • Redis解决方案:NOAUTH Authentication required(连接jedis绑定密码或修改redis密码)
  • 多维时序 | Matlab实现WOA-TCN-Multihead-Attention鲸鱼算法优化时间卷积网络结合多头注意力机制多变量时间序列预测
  • 如何实现无公网IP实现远程访问MongoDB文件数据库
  • 华为防火墙USG6000V1的NAT实验
  • spark-flink设计思想之吸星大法-1
  • 力扣1312. 让字符串成为回文串的最少插入次数
  • qemu的安装
  • myql入门
  • 前端开发有没有必要转鸿蒙开发?
  • 《动手学深度学习(PyTorch版)》笔记1
  • 前端工程化之:webpack1-5(配置文件)
  • 代码随想录栈和队列专题二刷复盘day17
  • 代码随想录算法刷题训练营day16
  • 【C语言/数据结构】排序(直接插入排序|希尔排序)
  • Jupyter Notebook安装使用教程
  • Unity 中的接口和继承
  • C++区间覆盖(贪心算法)
  • Python with Office 054 - Work with Word - 7-9 插入图像 (3)
  • Nodejs前端学习Day4_fs文件系统模块基础应用之成绩转换
  • 五、Kotlin 函数进阶
  • 重温《深入理解Java虚拟机:JVM高级特性与最佳实践(第二版)》 –– 学习笔记(一)
  • 定向减免!函数计算让轻量 ETL 数据加工更简单,更省钱
  • git checkout和git switch的区别
  • 故障树分析蒙特卡洛仿真程序(附MATLAB完整代码)