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

前端 未结 5 643
旧时难觅i
旧时难觅i 2020-12-28 14:36

I have downloaded eclipse and installed the spring suit into it. I have written a JPA based Rest application by following one of the spring.io guides. When I try to run it a

相关标签:
5条回答
  • 2020-12-28 14:49

    Ok, same problem i have faced , issue is due to version mismatch between spring boot and gson....solution is remove version

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <!-- Gson dependency -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    

    refer my pom.xml snip png

    0 讨论(0)
  • 2020-12-28 14:53

    You can exclude GsonAutoConfiguration

    @SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class})
    
    0 讨论(0)
  • 2020-12-28 15:00

    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.

    0 讨论(0)
  • 2020-12-28 15:06

    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.

    0 讨论(0)
  • 2020-12-28 15:10

    I think the problem is related to how Spring triggers the "AutoConfiguration". In this case the "GsonAutoConfiguration" is triggered on the presence of com.google.gson.Gson.class, even if you are not using it, as it has the @ConditionalOnClass(value=Gson.class) annotation.

    So the solution may be either removing the dependency that has Gson class or removing the specific "AutoConfiguration":

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

    https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.html

    0 讨论(0)
提交回复
热议问题