SpringCloud配置网关时运行项目出现Consider defining a bean of type ‘org.springframework.http.codec.ServerCodecCo

编程知识 更新时间:2023-05-02 05:31:00

错误详情

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-28 09:26:11.105 ERROR 19816 --- [           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.

错误分析

网关异常出现Consider defining a bean of type 'org.springframework.http.codec.ServerCodecConfigurer' in your configuration.要排除其他依赖的spring-boot-starter-web以及spring-boot-starter-webflux,因为会与spring-boot-starter-gatewaywebflux冲突

错误解决

将原本的gateway依赖

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

        </dependency>

修改为

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

            </exclusions>
        </dependency>

刷新pom.xml文件后,再次运行项目
修改前:

修改后:

问题解决

峰回路转

按照上面的操作去做,问题确实是解决了,运行也没有问题了,但是网关作用失效了
运行成功的上面,还是提示了

访问网关(网关配置正常)

访问结果,404

博主在此纠结了两天,一直以为是网关配置错误的原因,后面尝试着用了下面的方法,最终解决问题,也发现了前面方法的问题,是网关失效了(与配置文件无关,是环境的问题)

解决方法

由于spring-boot-starter-web依赖产生的问题,所以我们找到这个依赖是如何进入我们当前的模块的,只有两种方式:1.从父项目中继承;2.自身添加

  1. 首先检查本模块的pom.xml文件中有没有添加web依赖,若有,删除该依赖
  2. 找到父项目的pom.xml文件,查看是否有web依赖,若有,则删除该依赖,同时在其他需要web依赖的子项目中加上该依赖
  3. 由于web依赖已经被删除,此时需要将gateway依赖中的<exclusions>标签整体删除
  4. 刷新pom.xml文件夹
    完成上述步骤后,重启gateway对应的子模块

    此时,发现运行成功的上面已经没有了WARN提示

    其他都不用改,访问之前访问失败的地址

    发现访问成功,这也证明了,是环境的问题,不是我们配置的网关有问题
    至此,问题真正解决

更多推荐

SpringCloud配置网关时运行项目出现Consider defining a bean of type ‘org.springframework.http

本文发布于:2023-04-26 06:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/0e6986cbefce03c33071f4bff3697a4a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时运   网关   项目   bean   defining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 104564文章数
  • 26215阅读数
  • 0评论数