未见彩虹时,我知道我的雨季还没结束
背景
在开始学习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
解决方案
由两张图可知,服务启动时,是依赖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>