快捷搜索: 王者荣耀 脱发

spring cloud gateway网关启动报错(依赖冲突)

一、控制台报错代码

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

二、错误分析及解决办法

错误解决方法: 将pom.xml中关于spring-boot-start-web模块的jar依赖去掉。

错误分析: 根据上面描述(Description)中信息了解到GatewayAutoConfiguration这个配置中找不到ServerCodecConfig这个Bean。

因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web。

<!--gateway是依赖webflux的,所以这里应该倒入webflux,注释掉web-->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

三、整理总结maven版本冲突的解决办法: 举例 A依赖于B及C,而B又依赖于X、Y,而C依赖于X、M,则A除引B及C的依赖包下,还会引入X,Y,M的依赖包(一般情况下了,Maven可通过等若干种方式控制传递依赖)。 (解决该问题有四种原则:路径近者优先原则,第一声明者优先原则,这两种原则又统称为调节原则,还有排除原则和版本锁定原则。) 1、路径近者优先原则 2、第一声明者优先原则 3、排除原则 将冲突的依赖包括在和 标签里面 排除原则是在某个不用的坐标下添加exclusion标签

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

其实我个人觉得如果是我今天gateway遇到的情况,可以注释掉不用的依赖

<!--gateway是依赖webflux的,所以这里应该倒入webflux,注释掉web-->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

4、版本锁定原则 借鉴了博客:

经验分享 程序员 微信小程序 职场和发展