2

Prologue:

I have researched this topic from the stackoverflow answers. All answers were pointed towards the dependency conflict, which in my case doesn't seem to be true. (if i am not missing anything)

Problem:

I am getting this exception during runtime when i use the MongoTemplate implementation save or insert

mongoTemplate.save(saveObject, COLLECTION)
mongoTemplate.insert(insertObject, COLLECTION)

There are no build issues or compilation issues. The save or insert methods internally call MongoCollection<TDocument>.insertOne(TDocument doc) for my use case. If I implement this like below, it works fine :

MongoCollection<Document> collection = mongoTemplate.getCollection(COLLECTION);
collection.insertOne(docToSave);

Debug:

Also, If i put a debug point at org.springframework.data.mongodb.core.MongoTemplate

collectionToUse.insertOne(dbDoc);//exception is thrown from here

and execute it manually from the debugger, it works!

So there is no way that the Implementation is missing and judging from the gradle dependencies map, there seems to be no conflict as well.

here is my dependency structure:

+--- org.springframework.integration:spring-integration-mongodb -> 5.4.5
|    +--- org.springframework.integration:spring-integration-core:5.4.5
|    |    +--- org.springframework:spring-aop:5.3.5 (*)
|    |    +--- org.springframework:spring-context:5.3.5 (*)
|    |    +--- org.springframework:spring-messaging:5.3.5
|    |    |    +--- org.springframework:spring-beans:5.3.5 (*)
|    |    |    \--- org.springframework:spring-core:5.3.5 (*)
|    |    +--- org.springframework:spring-tx:5.3.5
|    |    |    +--- org.springframework:spring-beans:5.3.5 (*)
|    |    |    \--- org.springframework:spring-core:5.3.5 (*)
|    |    +--- org.springframework.retry:spring-retry:1.3.1
|    |    \--- io.projectreactor:reactor-core:3.4.4
|    |         \--- org.reactivestreams:reactive-streams:1.0.3
|    \--- org.springframework.data:spring-data-mongodb:3.1.6
|         \--- org.springframework.data:spring-data-commons:2.4.6
+--- org.mongodb:mongo-java-driver:3.12.9

gradle file:

//project sensitive configurations omiited
..
//


plugins {
    id 'org.springframework.boot' version '2.4.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'war'
}

//project sensitive configurations omiited
..
//

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.mongodb', module: 'mongodb-driver-core'
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation group: 'javax.servlet', name: 'jstl', version: '1.2'
    implementation group: 'javax.servlet', name: 'javax.servlet-api'
    providedRuntime group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.44'
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') {
        exclude group: 'org.mongodb', module: 'mongodb-driver-core'
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }
    providedRuntime group: 'javax.servlet.jsp', name: 'javax.servlet.jsp-api', version: '2.3.3'
    implementation('org.springframework.integration:spring-integration-mongodb') {
        exclude group: 'org.mongodb', module: 'mongodb-driver-core'
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }
    implementation 'org.mongodb:mongo-java-driver:3.12.9'
    implementation 'org.apache.httpcomponents:httpclient:4.4'
    implementation('com.jayway.jsonpath:json-path:2.0.0') {
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }
    implementation 'javax.mail:mail:1.4'
    implementation 'com.googlecode.json-simple:json-simple:1.1.1'
    implementation 'org.reflections:reflections:0.9.9-RC1'
    implementation 'commons-logging:commons-logging:1.2'
    implementation 'commons-fileupload:commons-fileupload:1.3.1'
    implementation 'commons-io:commons-io:2.4'
    implementation 'com.jayway.jsonpath:json-path-assert:0.8.1'
    implementation 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
    implementation 'org.json:json:20141113'
    implementation 'org.apache.poi:poi:3.15'
    implementation 'org.apache.xmlbeans:xmlbeans:3.0.2'
    implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
    implementation 'org.apache.poi:poi-ooxml:3.15'
    implementation 'com.auth0:java-jwt:2.1.0'
    implementation 'net.sf.jxls:jxls-reader:1.0.6'
    implementation 'com.efsavage.jquery:jquery-maven:1.7.2'
    implementation('cglib:cglib:2.2.2') {
        exclude group: 'asm', module: 'asm'
    }
    implementation('org.springframework:spring-mock:2.0.8')
            {
                exclude group: 'org.slf4j', module: 'slf4j-simple'
                exclude group: 'org.slf4j', module: 'slf4j-api'
            }
    implementation 'xerces:xercesImpl:2.11.0'
    implementation 'com.google.guava:guava:18.0'
    implementation 'org.apache.commons:commons-lang3:3.4'
    implementation 'info.cukes:cucumber-picocontainer:1.2.2'
    implementation 'info.cukes:cucumber-junit:1.2.2'
    implementation 'info.cukes:cucumber-java:1.2.2'
    implementation 'javax:javaee-web-api:7.0'

    //project sensitive configurations omiited
    ..
    //
}


//project sensitive configurations omiited
..
//

Exception:

2021-22-07 13:16:03 [http-nio-8081-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] -
                Servlet.service() for servlet [dispatcherServlet] in context with path [/MY_ROOT_PATH] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: 'com.mongodb.client.result.InsertOneResult com.mongodb.client.MongoCollection.insertOne(java.lang.Object)'] with root cause
java.lang.NoSuchMethodError: 'com.mongodb.client.result.InsertOneResult com.mongodb.client.MongoCollection.insertOne(java.lang.Object)'
    at org.springframework.data.mongodb.core.MongoTemplate.lambda$saveDocument$18(MongoTemplate.java:1505)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:561)
    at org.springframework.data.mongodb.core.MongoTemplate.saveDocument(MongoTemplate.java:1492)
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:1428)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1370)
    at com.company.package.dao.mongo.MyDAOImpl.appendAndSave(MyDAOImpl.java:78)
    at com.company.package.dao.mongo.MyDAOImpl.saveFile(MyDAOImpl.java:50)
5
  • could you check dependency jar files, maybe there is more then one mongodb-driver jar file ? Commented Jul 22, 2021 at 8:51
  • @muhammedozbilici I did check that. There is just one mongo-java-driver-3.12.9.jar Commented Jul 22, 2021 at 9:49
  • 2
    And why do you try to exclude mongodb-driver-core and use your own dependecny for it instead of relying on whatever Spring Boot brings you as compatible version? Commented Jul 22, 2021 at 12:44
  • This was an old spring-mvc project with deprecated mongo. I am working on the DB migration and upgrade (server 3.4.21 to 4.2.12) with minimal code changes possible (upgrade to Spring Boot as well). Most of the DB operation were implemented around mongo-java-driver hence I am using it exclusively. Including mongodb-driver-core is not making any differences either, or causing any noticeable conflicts. Also, spring is not bringing the mongo-java-driver from the implementations I have in build.gradle. Hence, I just excluded mongodb-driver-core just to be sure of any chances of conflicts. Commented Jul 22, 2021 at 14:00
  • @ArtemBilan I thought about what you said and found out spring-boot-starter-data-mongodb brings down all the implementations the project required, including the implementations from org.mongodb:mongo-java-driver as org.mongodb:mongo-driver-sync. removing spring-integration-mongodb and mongo-java-driver and using just spring-boot-starter-data-mongodb fixed the issue! I think it's safe to assume that spring is looking for a compatible org.mongodb:mongo-driver-sync implementation. However, I would like to hear other's opinion on this one. Commented Jul 22, 2021 at 15:20

1 Answer 1

3

Removing spring-integration-mongodb and mongo-java-driver and using just spring-boot-starter-data-mongodb fixed the issue! I think it's safe to assume that spring is looking for a compatible org.mongodb:mongo-driver-sync implementation. However, I would like to hear other's opinion on this one.

FYI, spring-boot-starter-data-mongodb brings down all the implementations the project required, including the implementations from org.mongodb:mongo-java-driver as org.mongodb:mongo-driver-sync.

the changed build.gradle :

//project sensitive configurations omitted
...
//

plugins {
    //project sensitive configurations omitted
    ..
    //
    id 'org.springframework.boot' version '2.4.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'war'
}

//project sensitive configurations omitted
...
//

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }
    implementation group: 'javax.servlet', name: 'jstl', version: '1.2'
    implementation group: 'javax.servlet', name: 'javax.servlet-api'
    providedRuntime group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.44'
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') {
        exclude group: 'org.slf4j', module: 'slf4j-simple'
        exclude group: 'org.slf4j', module: 'slf4j-api'
    }
    providedRuntime group: 'javax.servlet.jsp', name: 'javax.servlet.jsp-api', version: '2.3.3'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb'


    // omitted unrelated dependencies for readability
    ...
    //

    //project sensitive configurations omitted
    ...
    //

}

//project sensitive configurations omitted
...
//
Sign up to request clarification or add additional context in comments.

1 Comment

That's correct solution and right assumption. And that's indeed one of the main points of Spring Boot: to bring for your all the required and compatible dependencies and make your target burden about version management much easier. I can't find at the moment exact sentence about dependency management, but here is something closer than nothing: docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…. You can accept your own answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.