背景
在使用powerjob時引入第三方jar包(odps)后SpringBoot啟動報錯
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gsonBuilder' defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.gson.GsonBuilder]: Factory method 'gsonBuilder' threw exception; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
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:/C:/Users/gjs/.m2/ctgrepository/com/google/code/gson/gson/2.2.4/gson-2.2.4.jar!/com/google/gson/GsonBuilder.class It was loaded from the following location: file:/C:/Users/gjs/.m2/ctgrepository/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
解決方法
The following method did not exist: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
報錯信息中提示找不到 GsonBuilder的setLenient()方法,很明顯是jar包版本沖突了,gson版本太低了
可以去maven倉庫查看SpringBoot所需對應的gson版本
https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
我使用的SpringBoot版本是2.2.6.RELEASE
SpringBoot 2.2.6.RELEASE 需要使用2.8.6以上的gson,而引入的odps依賴的是2.2.4
一般jar包都可以向上兼容,所以直接在pom文件中直接引入2.8.6以上的gson替換即可,不需要去改變odps的版本
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>