You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Data Mongo与LangChain4j Mongo Atlas的Maven依赖冲突问题

解决Spring Boot 3.3.6集成LangChain4j MongoDB Embedding Store的依赖兼容性问题

问题场景

Spring Boot 3.3.6项目集成LangChain4j,使用MongoDB作为Embedding存储实现RAG,启动时触发方法不存在错误,具体报错信息如下:

APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore.isCollectionExist(MongoDbEmbeddingStore.java:337)

The following method did not exist:

    'com.mongodb.client.MongoIterable com.mongodb.client.MongoDatabase.listCollectionNames()'

The calling method's class, dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore, was loaded from the following location:

    jar:file:/home/cholordo/.m2/repository/dev/langchain4j/langchain4j-mongodb-atlas/0.36.2/langchain4j-mongodb-atlas-0.36.2.jar!/dev/langchain4j/store/embedding/mongodb/MongoDbEmbeddingStore.class

The called method's class, com.mongodb.client.MongoDatabase, is available from the following locations:

    jar:file:/home/cholordo/.m2/repository/org/mongodb/mongodb-driver-sync/5.0.1/mongodb-driver-sync-5.0.1.jar!/com/mongodb/client/MongoDatabase.class

Action:

Correct the classpath of your application so that it contains compatible versions of the classes dev.langchain4j.store.embedding.mongodb.MongoDbEmbeddingStore and com.mongodb.client.MongoDatabase

错误触发于创建Embedding Store的Bean代码:

@Bean
public EmbeddingStore embeddingStore(){
    String uri = "mongodb://root:secret@172.17.0.2:27017/admin?authSource=admin";
    MongoClient mongoClient = MongoClients.create(uri);
    return new MongoDbEmbeddingStore.Builder().indexName("wildcatProIndex")
            .fromClient(mongoClient)
            .collectionName("ingested_documents")
            .databaseName("admin")
            .build();
}

错误原因

  1. LangChain4j版本不一致:项目中langchain4j-spring-boot-starter使用0.32.0版本,其他LangChain4j依赖(如langchain4j-mongodb-atlas)使用0.36.2版本,版本差异导致内部依赖逻辑不兼容。
  2. MongoDB驱动版本过低:Spring Boot 3.3.6默认引入MongoDB驱动mongodb-driver-sync 5.0.1,而LangChain4j 0.36.2依赖的MongoDB驱动版本要求更高(6.x及以上),MongoDatabase.listCollectionNames()方法的签名在高版本驱动中才匹配LangChain4j的调用要求。

解决方案

1. 统一LangChain4j依赖版本

将所有LangChain4j相关依赖的版本统一为0.36.2,避免版本冲突:

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-spring-boot-starter</artifactId>
    <version>0.36.2</version> <!-- 与其他LangChain4j版本保持一致 -->
</dependency>

2. 升级MongoDB驱动版本

通过Spring Boot配置将MongoDB驱动版本升级到与LangChain4j 0.36.2兼容的版本(推荐6.3.0及以上):

方式一:通过properties配置

在pom.xml的<properties>节点中添加:

<properties>
    <mongodb-driver.version>6.3.0</mongodb-driver.version>
</properties>

方式二:通过dependencyManagement覆盖

在pom.xml中添加依赖管理节点:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>6.3.0</version>
        </dependency>
    </dependencies>
</dependencyManagement>

3. 检查依赖冲突

执行Maven命令查看依赖树,确认没有低版本MongoDB驱动被引入:

mvn dependency:tree | grep mongodb

若存在冲突,手动在对应依赖中排除低版本MongoDB驱动。

验证步骤

  1. 执行mvn clean install重新构建项目
  2. 启动Spring Boot应用
  3. 检查Embedding Store Bean是否正常初始化,无方法不存在错误

内容的提问来源于stack exchange,提问作者Agustn Ernesto Cardeilhac Bans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:42:02