Why do I get Gson builder error when starting a Spring Boot application?

I would write this as a comment, but I still haven't got enough rep.

The problem must be with your dependencies. What happens here is that SpringBoot loads the GsonAutoConfiguration @Configuration class, which tries to call GsonBuilder's setLenient() method. SpringBoot already has the correct gson jar set as a dependency which should automatically be included in your build; however, explicitly specifying a dependency to gson would override the dependency brought by SpringBoot. Apparently, setLenient() still did not exist in the version of gson that you are using.

The best you can do is either remove the explicit dependency to gson from your pom.xml (or build.gradle, or whatever else you use), or update it to match the one which is expected by the SpringBoot version you are using.

This is the most recent version of gson, in case you are using a recent version of SpringBoot:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>

EDIT: it could also happen that even if you don't declare gson explicitly in your build, another one of your dependencies is using an older version of it, and it overrides the version which SpringBoot expects. In that case, instead of trying to brute-force through the problem, I would suggest to go through all your dependencies and make sure that the versions line up. Going through the dependencies and their versions listed in Maven Central might be a good idea.


I faced the same problem and had to waste a lot of time trying to fix this.

The problem arises due to the version mismatch of the Gson library from existing dependencies already included in your project with that of Spring Boot's default one.

The easiest fix of this problem (that worked for me) is to replace each occurrence of the

@EnableAutoConfiguration

tag with

@EnableAutoConfiguration(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})

which basically tells the Spring boot application to skip auto configuration for Gson.

This solution also applies to any other class that might create the same problem. You just need to add the name of each such conflicting class to the exclude attribute of EnableAutoConfiguration.