How to resolve -- NoSuchMethodError: com.mongodb.client.MongoCollection.insertMany

I’m working on springboot mvc gradle project. I upgraded some versions due to which now when I’m running the api to connect and save data in my mongodb. It gives me this error –

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: com.mongodb.client.MongoCollection.insertMany(Ljava/util/List;)Lcom/mongodb/client/result/InsertManyResult;] with root cause

java.lang.NoSuchMethodError: com.mongodb.client.MongoCollection.insertMany(Ljava/util/List;)Lcom/mongodb/client/result/InsertManyResult;
    at org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocumentList$17(MongoTemplate.java:1491) ~[spring-data-mongodb-3.3.1.jar:3.3.1]
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:555) ~[spring-data-mongodb-3.3.1.jar:3.3.1]

I tried to resolve some version conflict. But the thing is I don’t see any mongodb version conflict. Can someone help me to explain what I wrong and how it can be resolved.

org.springframework.boot:spring-boot-starter-data-mongodb:2.6.3
|    +--- org.springframework.boot:spring-boot-starter:2.6.3 (*)
|    +--- org.mongodb:mongodb-driver-sync:4.4.1
|    |    +--- org.mongodb:bson:4.4.1
|    |    \--- org.mongodb:mongodb-driver-core:4.4.1
|    |         \--- org.mongodb:bson:4.4.1
|    \--- org.springframework.data:spring-data-mongodb:3.3.1
|         +--- org.springframework:spring-tx:5.3.15 (*)
|         +--- org.springframework:spring-context:5.3.15 (*)
|         +--- org.springframework:spring-beans:5.3.15 (*)
|         +--- org.springframework:spring-core:5.3.15 (*)
|         +--- org.springframework:spring-expression:5.3.15 (*)
|         +--- org.springframework.data:spring-data-commons:2.6.1
|         |    +--- org.springframework:spring-core:5.3.15 (*)
|         |    +--- org.springframework:spring-beans:5.3.15 (*)
|         |    \--- org.slf4j:slf4j-api:1.7.32 -> 1.7.33
|         +--- org.mongodb:mongodb-driver-core:4.4.1 (*)

build.gradle –

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.6.3")
    }
}
dependencyManagement {
    imports {
        mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
    }
}

dependencies {
   implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-jdbc")
    implementation 'org.apache.avro:avro:1.9.1'
    implementation 'org.json:json:20171018'
    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
    implementation 'com.amazonaws:aws-java-sdk-s3'
    implementation "org.apache.kafka:kafka-clients:3.1.0"
    implementation(group: 'commons-io', name: 'commons-io', version: '2.11.0')
    implementation group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.8.2'
    testImplementation "junit:junit"
    implementation 'com.github.javadev:underscore:1.47'
    implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'
    implementation 'org.mongodb:mongo-java-driver:3.12.10'
    implementation 'com.h2database:h2:1.4.200'
    implementation 'com.splunk:splunk:1.6.5.0'
    implementation 'com.google.guava:guava:13.0.1'
    implementation("org.springframework.boot:spring-boot-starter-batch")
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation group: 'com.opencsv', name: 'opencsv', version: '4.6'
    implementation 'org.springframework.boot:spring-boot-starter:2.6.3'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb:2.6.3'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.batch:spring-batch-test'
}

Hi @Vartika_Malguri ,

This dependency is probably what is causing the problem:

Do you know where it’s coming from, and can you exclude it?

Regards,
Jeff

Hey @Jeffrey_Yemin

When I removed it – the application doesn’t work(I guess it’s Important )
Someone suggested me to use – implementation ‘org.mongodb:mongo-java-driver:3.11.2’
Spring Data MongoDB builds on top of MongoDB’s Java driver. It uses it to communicate with a MongoDB instance. To use Spring Data MongoDB you need both it and the Java driver and accroding to the Changlog the last supported version is 3.11.2 .
But even this doesn’t work …it gave me this error –

An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.boot.autoconfigure.mongo.MongoPropertiesClientSettingsBuilderCustomizer.applyUuidRepresentation(MongoPropertiesClientSettingsBuilderCustomizer.java:67) The following method did not exist: com.mongodb.MongoClientSettings$Builder.uuidRepresentation(Lorg/bson/UuidRepresentation;)Lcom/mongodb/MongoClientSettings$Builder

I guess the – springframework.boot:spring-boot-starter-data-mongodb:2.6.3 is also using –

org.mongodb:mongodb-driver-core:4.4.1 (which is also creating conflict)and the other issue I mentioned above also , it's showing -- Correct the classpath of your application so that it contains compatible versions of the classes -- org.springframework.boot.autoconfigure.mongo.MongoPropertiesClientSettingsBuilderCustomizer and com.mongodb.MongoClientSettings$Builder

Spring Boot 2.6.3 depends on the 4.4.x line of the driver and so does Spring Data MongoDB in version 3.3.1. As @Jeffrey_Yemin already mentioned it’s likely there’s an invalid dependency to an earlier version of the driver.
Do you have a Minimal, Reproducible Example that reproduces the error?

Oh okay …what should I specify sir
Like the error occur when I’m trying to save the data into my local mongodb.
Maybe I’m not elaborative. I also think so it’s due to dependency issue.

Let me be very precise that. how this issue came –

The application was fine before I was using the older version of springboot
, mongodb and spring-boot-gradle-plugin was 2.2.5.RELEASE .
Then I wrote a query where I have to use UnionOperator of mongodb but it was 
not supported in the older version.
So, I upgraded  spring-boot-gradle-plugin  to the latest version (major change
was the spring-boot-gradle-plugin because when I changed it to the latest or
 above 2.3.12.RELEASE then I was able to use the UnionOperator
But then It started showing me errors. 

To use unionOperator I was forced to change the “spring-boot-gradle-plugin” 
because I upgraded springboot-mongodb and mongodb-driver to it’s latest but 
that didn't work .
And then I upgraded  spring-boot-gradle-plugin which works but then, it showed me this error

Hey @Jeffrey_Yemin
When I excluded it – it showed me this error –

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'mongoDBConfig' defined in file

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.