今天启动我的spring-boot项目时报了如下的错误:
代码语言:javascript复制***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
java.lang.invoke.MethodHandleNatives.resolve(Native Method)
The following method did not exist:
com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
The method's class, com.google.gson.GsonBuilder, is available from the following locations:
jar:file:/J:/maven_repository/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar!/com/google/gson/GsonBuilder.class
The class hierarchy was loaded from the following locations:
com.google.gson.GsonBuilder: file:/J:/maven_repository/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.gson.GsonBuilder
意思就是找不到com.google.gson.GsonBuilder.setLenient()
方法,gson-2.2.4.jar
中的GsonBuilder
类确没有这个方法。初步判断这应该是gson库的版本不匹配造成的
google上找了一下,找到stackoverflow上的如下帖子:
《Why do I get Gson builder error when starting a Spring Boot application?》
知道了原因,org.springframework.boot:spring-boot-autoconfigure:2.5.6
包中的GsonAutoConfiguration类中引用了com.google.gson.GsonBuilder.setLenient()
‘方法,应该是较高的gson版本。
我的项目中引用的org.apache.phoenix:phoenix-core:4.14.0-cdh5.14.2
库也依赖gson 2.2.4库,替换了spring-boot-autoconfigure
中引用的gson高版本,导致错误发生。
知道了原因,就知道怎么解决了:就要在项目强制使用spring-boot依赖的高版本。
如上面stackoverflow的帖子中的解决办法 ,在项目的pom.xml中添加gson依赖,使高版本生效
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
参考资料
《Why do I get Gson builder error when starting a Spring Boot application?》