前言
今日调试一个基于Spring Cloud Gateway网关工程,估计是“代码放久”了,难免出点问题。
一、报错信息
[traceCode:] [main] ERROR o.s.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.youngdatafan.gateway.GatewayApplication]; nested exception is java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurer.class] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/web/servlet/config/annotation/WebMvcConfigurer.class] cannot be opened because it does not exist
...
根据报错信息,及网上找到的相关修复方法,添加相关依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
再次运行,报错信息如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.
二、调试解决
根据报错信息,再查找一下解决办法,大多人的意见说是“Spring MVC与Spring Cloud网关不兼容,需删除spring-boot-start-web依赖项”。
按照这样的说法,那第一步就不该添加“spring-boot-starter-web”这个依赖项。那问题来了,不添加这个依赖项,那就到不了第二步。
接着只能开始各种依赖项排查调试…可是并未解决问题。
“网关工程”中有些“老代码”引用到“spring-boot-starter-web”相关类,因此不能去掉此依赖项。
最后查找多种解决办法,在配置文件“application.yaml”中添加配置项解决此问题。
spring.main.web-application-type=reactive
总结
因此,只能说错误是随不同环境而不同的,相关解决办法只能算是一种“思路”,综合多种“思路”解决问题。这也对“开源组件”的工作机制原理的掌握提出更高的要求。