2

I am writing a new Spring Boot application and I need to use one package which has been developed by my team. This package has older Gson dependency somewhere down the hierarchy (which I could not find), therefore while running my application following error was thrown :

NoSuchMethodError: com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder.

I tried to

  1. Specify the Gson dependency exclusively in my pom, so as to enforce the latest version
  2. Add the dependency in DependencyManagement section

  3. Exclude the Gson dependency as follows :

    <dependency>
     <groupId>com.mycompany</groupId>
     <artifactId>services</artifactId>
     <version>${version}</version>
     <exclusions>
        <exclusion>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </exclusion>
     </exclusions>
    </dependency>
    

All three options did not work. finally I was able to get my application running by excluding GsonAutoConfiguration.

Questions I am desperately trying to understand here:

  1. Why the earlier options did not work
  2. If I am skipping Gson AutoConfiguration, what am I losing and how can I achieve that?

Any help in this regard will be highly appreciated. Thanks in advance.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany</groupId>
<artifactId>myapp</artifactId>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>


<properties>
    <java.version>1.8</java.version>
    <spring.boot.version>2.1.7.RELEASE</spring.boot.version>
    <camel.version>2.24.1</camel.version>
    <lasius.wsutils.version>3.0.37</lasius.wsutils.version>
    <mysql.connector.version>5.1.47</mysql.connector.version>
</properties>

<!--<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.7.RELEASE</version>
</parent>-->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-dependencies</artifactId>
            <version>${camel.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


<dependencies>
    <!--Spring Boot dependencies-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>-->
    <!-- camel dependencies -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-undertow-starter</artifactId>
    </dependency>

    <!-- test dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--being resolved from auth-->
    <!--<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.9.3</version>
    </dependency>-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.connector.version}</version>
    </dependency>


    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>services</artifactId>
        <version>${version}</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.boot.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>
Vishal Maral
  • 1,279
  • 1
  • 10
  • 30
  • you can look at the AutoConfiguration implementation: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.java - usually it bootsstraps a bean and some properties for your applicationContext. You can do that yourself in your configurations. But I don't understand why your option 1 did not work. Did you add it into the right module? The shortest path algorithm of maven should have resolved to that dependency. You did use a newer json version or an older one? – wemu Sep 22 '19 at 17:03
  • share your pom so we can check ... – Nghia Do Sep 22 '19 at 17:32
  • @wemu I added the newer version as shown above in pom.xml – Vishal Maral Sep 23 '19 at 07:20
  • maybe "mvn depedency:tree" can give a hint why adding the dependency directly has no effect. – wemu Sep 23 '19 at 10:49
  • I am having the same issue. I have also tried all three of those fixes with no luck. Did anyone find a solution for this? – Ryan Conway Nov 24 '20 at 14:15

0 Answers0