Home > Enterprise >  NoClassDefFoundError on MongoCustomConversions while upgrading springboot
NoClassDefFoundError on MongoCustomConversions while upgrading springboot

Time:04-24

I am getting the following error

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.mongodb.core.convert.MongoCustomConversions

I am upgrading springboot to 2.6.6,springframework to 5.3.18, I have also added spring-data-mongodb dependency version : 3.3.3

Detailed stack trace :

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.mongodb.core.convert.MongoCustomConversions at org.springframework.boot.autoconfigure.data.mongo.MongoDataConfiguration.mongoCustomConversions(MongoDataConfiguration.java:63) at sun.reflect.GeneratedMethodAccessor342.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 75 more

spring dependencies in pom are :

   <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <version>2.6.6</version>
    </dependency>
   <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.3.18</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.18</version>
     </dependency>
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.18</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>compile</scope>
     </dependency>
     <dependency>
        <groupId>org.springframework.hateoas</groupId>
        <artifactId>spring-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.3.18</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jcl</artifactId>
        <version>5.3.18</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.6.6</version>
        <scope>test</scope>
     </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>3.3.3</version>
    </dependency>

    and in properties file I have added :

    <spring-data-commons.version>2.6.0</spring-data-commons.version>
    <spring-data-mongodb.version>3.3.3</spring-data-mongodb.version>
    <spring-boot.version>2.6.6</spring-boot.version>
    <spring.version>${spring-boot.version}</spring.version>

CodePudding user response:

Hi This error got resolved after adding the following dependency :

 <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>bson</artifactId>
        <version>4.6.0</version> </dependency`

this is the latest bson version, you can add a version compatible with your other depen

CodePudding user response:

Can you please list all the dependencies in your program, with below snippets from my pom.xml, I am able to use MongoCustomConversions without any issues.

Spring boot: 2.6.6 Spring framework: 5.3.18 spring-data-mongodb: 3.3.3

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
  ....
  
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

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

  • Related