【问题标题】:Spring Boot: MongoDb: Exception: NoSuchMethodError: com.mongodb.client.MongoCollection.insertOne(java.lang.Object)Spring Boot:MongoDb:异常:NoSuchMethodError:com.mongodb.client.MongoCollection.insertOne(java.lang.Object)
【发布时间】:2025-12-30 09:50:06
【问题描述】:

序幕:

我从 * 的答案中研究了这个主题。所有答案都指向依赖冲突,在我的情况下这似乎不是真的。 (如果我没有遗漏任何东西

问题:

当我使用MongoTemplate 实现saveinsert 时,我在运行时 遇到此异常

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

没有构建问题或编译问题。 对于我的用例,saveinsert 方法在内部调用 MongoCollection<TDocument>.insertOne(TDocument doc)。如果我像下面这样实现它,它工作正常:

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

调试:

另外,如果我将调试点放在org.springframework.data.mongodb.core.MongoTemplate

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

并从调试器手动执行它,它可以工作!

因此不可能缺少实现,从gradle dependencies映射来看,似乎也没有冲突。

这是我的依赖结构:

+--- 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 文件:

//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
..
//

例外:

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)

【问题讨论】:

  • 你能检查一下依赖jar文件吗,也许有不止一个mongodb-driver jar文件?
  • @muhammedozbilici 我确实检查过。只有一个mongo-java-driver-3.12.9.jar
  • 你为什么要尝试排除mongodb-driver-core并使用你自己的依赖而不是依赖Spring Boot为你带来的任何兼容版本?
  • 这是一个旧的 spring-mvc 项目,mongo 已弃用。我正在进行数据库迁移和升级(服务器 3.4.21 到 4.2.12),代码更改最少(也升级到 Spring Boot)。大多数数据库操作都是围绕mongo-java-driver 实现的,因此我只使用它。包括mongodb-driver-core 也不会产生任何差异,也不会导致任何明显的冲突。此外,spring 并没有从我在build.gradle 中的实现中带来mongo-java-driver。因此,我只是排除了mongodb-driver-core 只是为了确定任何冲突的可能性。
  • @ArtemBilan 我考虑了你所说的,发现spring-boot-starter-data-mongodb 降低了项目所需的所有实现,包括来自org.mongodb:mongo-java-driver 的实现org.mongodb:mongo-driver-sync。删除 spring-integration-mongodbmongo-java-driver 并仅使用 spring-boot-starter-data-mongodb 解决了这个问题!我认为可以安全地假设 spring 正在寻找兼容的 org.mongodb:mongo-driver-sync 实现。不过,我想听听其他人对此的看法。

标签: java mongodb spring-boot spring-integration


【解决方案1】:

删除spring-integration-mongodbmongo-java-driver 并仅使用spring-boot-starter-data-mongodb 解决了这个问题!我认为可以安全地假设 spring 正在寻找兼容的org.mongodb:mongo-driver-sync 实现。不过,我想听听其他人对此的看法。

仅供参考, spring-boot-starter-data-mongodb 降低了项目所需的所有实现,包括来自 org.mongodb:mongo-java-driver 的实现为 org.mongodb:mongo-driver-sync

更改后的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
...
//

【讨论】:

  • 这是正确的解决方案和正确的假设。这确实是 Spring Boot 的要点之一:为您带来所有必需且兼容的依赖项,并使您的目标版本管理负担更加轻松。目前我找不到关于依赖管理的确切句子,但这里有一些比没有更接近的东西:docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…。您可以接受自己的答案。