3
votes

I am using Spring Boot to implement a spark application. I build an uber jar, and use spark-submit to execute the application. But I got the following exception:

18/06/05 15:32:37 ERROR SpringApplication: Application run failed 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.BootstrapMethod

Error: java.lang.NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1254) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1103) .....

The exception is still there even if I put the following dependency in my pom.xml:

<dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.5</version>
   <scope>compile</scope>           
</dependency>

How can I resolve this dependency issue?

1
Check your mvn dependency:tree to verify that you are not getting multiple gson libs. - Compass
do you call setLenient? If so, you might want to check the version actually used in your build, spring-boot's dependency management sometimes overrides dependency versions and maybe puts one that doesn't have this method yet/anymore. And if you don't call that method, it might be autoconfiguration code of boot in which case you may have luck not putting a <version> so boot gets the version it wants. Sidenote, boot uses jackson all over the place, maybe you can also use that instead of gson - zapl
I checked installed spark jar folder, and found a gson-2.2.4.jar which doesn't have setLenient method. It seems that this jar file is used for running application although I build a uber jar. - Xin Wu

1 Answers

4
votes

Maybe you can use @SpringBootApplication(exclude = {GsonAutoConfiguration.class}) to disable gson autoconfiguration.