Eureka踩坑事件-An attempt was made to call the method GsonBuilder.setLenient

未见彩虹时,我知道我的雨季还没结束

背景

在开始学习SpringCloud的Eureka时,在启动Eureka服务端时,出现以下错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-02 23:06:20.178 ERROR 8800 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; but it does not exist. Its class, com.google.gson.GsonBuilder, is available from the following locations:

    jar:file:/D:/mavenRepository/com/google/code/gson/gson/2.1/gson-2.1.jar!/com/google/gson/GsonBuilder.class

It was loaded from the following location:

    file:/D:/mavenRepository/com/google/code/gson/gson/2.1/gson-2.1.jar

Action:

Correct the classpath of your application so that it contains a single, compatible version of com.google.gson.GsonBuilder

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lCLSlJUX-1604331612075)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/059365b2-4dbc-4b11-b8f2-6f41a91fbba6/Untitled.png)]

解决方案

由两张图可知,服务启动时,是依赖gson,并且调用了GsonBuilder中的setLenient方法,但是在gson2.1版本中没有该方法,所以需要去替换成更高的版本,同时这里我们通过idea中的依赖图可以发现,gson的引入是在netflix中,我这里是使用的是netflix的eureka,所以就需要排除eureka中的gson,并且引入更高版本的gson

<dependencies>
        <!--Eureka server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <exclusions>
                <!-- 排除gson2.1-->
                <exclusion>
                    <groupId>com.google.code.gson</groupId>
                    <artifactId>gson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--        引入gson 2.8.4-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.4</version>
        </dependency>
    </dependencies>
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值