SpringCloudGateway和SpringBootStarterWeb依赖冲突问题解决

按照网上尝试过的教程,在父pom工程的<dependencyManagement>中去除spring-boot-starter-web依赖,并在需要此依赖的子模块中重新以GAV坐标的方式引用无法解决问题,以下是解决问题的方法。

问题复现
spring boot版本和spring cloud版本
框架 版本
SpringBoot 2.3.0.RELEASE
SpringCloud Hoxton.SR5
gateway中的依赖
	<dependency>
    	<groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
依赖冲突

**********************************************************

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

**********************************************************


2020-07-21 10:12:27.253  WARN 8576 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'gatewayControllerEndpoint' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration$GatewayActuatorConfiguration.class]: Unsatisfied dependency expressed through method 'gatewayControllerEndpoint' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'modifyRequestBodyGatewayFilterFactory' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAutoConfiguration.class]: Unsatisfied dependency expressed through method 'modifyRequestBodyGatewayFilterFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.http.codec.ServerCodecConfigurer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {
   }
2020-07-21 10:12:27.253  INFO 8576 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-07-21 10:12:27.273  INFO 8576 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-21 10:12:27.388 ERROR 8576 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
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.

2020-07-21 10:12:27.398  WARN 8576 --- [           main] o.s.boot.SpringApplication               : Unable to close ApplicationContext

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Unsatisfied dependency expressed through method 'springApplicationAdminRegistrar' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {
   }
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.
### 解决Spring Cloud GatewaySpringDoc的冲突问题 当遇到Spring Cloud GatewaySpringDoc之间存在兼容性问题时,通常是因为两者对于API文档路径处理机制的不同所引起的。具体来说,在某些情况下,默认配置可能会导致请求被错误路由或拦截。 一种常见的解决方案是在应用中自定义`WebMvcEndpointHandlerMapping` Bean来调整端点映射逻辑[^3]。通过这种方式可以改变原有的路径匹配模式,从而避免潜在的冲突。下面是一个具体的实现方法: #### 自定义WebMvcEndpointHandlerMapping Bean 创建一个新的Java类用于重新定义`WebMvcEndpointHandlerMapping` Bean,确保其采用不同的路径匹配器(例如AntPathMatcher),而不是默认的方式。这可以通过继承原有类并重写相应的方法来完成。 ```java import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; import org.springframework.boot.actuate.endpoint.annotation.ControllerEndpointsSupplier; import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class CustomWebMvcEndpointHandlerMapping { @Bean public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping( ObjectProvider<List<ExposableControllerEndpoint>> endpointProviders, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsConfiguration corsConfiguration, WebEndpointProperties webEndpointProperties) { List<ExposableControllerEndpoint> endpoints = new ArrayList<>(); endpointProviders.ifAvailable(endpoints::addAll); return new WebMvcEndpointHandlerMapping(webEndpointProperties.getBasePath(), endpoints, controllerEndpointsSupplier, endpointMediaTypes, corsConfiguration, new AntPathMatcher()); } } ``` 上述代码片段展示了如何通过注入必要的依赖项来自定义`WebMvcEndpointHandlerMapping`实例,并指定使用`AntPathMatcher`来进行路径匹配操作。 此外,如果正在使用的SpringCloud版本较新,则可能还需要考虑集成最新版的springdoc-openapi库以获得更好的支持[^2]。确保项目中的依赖关系是最新的可以帮助减少因不同模块间不一致而引发的问题。 最后,针对特定场景下的特殊需求,还可以进一步探索其他可选方案,比如调整gateway的过滤链设置或是修改openapi生成的相关参数等措施。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值