spring-boot:The following method did not exist: com.google.gson.GsonBuilder.setLenient()

2022-11-06 09:47:03 浏览数 (1)

今天启动我的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依赖,使高版本生效

代码语言:javascript复制
		<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?》

0 人点赞