当前位置: 首页 > 知识库问答 >
问题:

在Mongo驱动程序3.11.0中使用Spring Boot Starter Web依赖时出现Java . lang . nosuchmethod错误

郤立果
2023-03-14

将Spring Boot Starter Web依赖项与最新的MongoDB驱动程序3.11.0一起使用会给出以下错误。当我切换到旧版本的Mongo驱动程序(如3.8.2)时,这工作正常。有什么想法吗?我应该排除 Mongo 依赖项吗?

如果我删除Spring靴依赖项,Mongo连接可以正常工作。

06:39:09.046 [main] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[host1:27017, host2:27017, host3:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
06:39:09.048 [main] INFO org.mongodb.driver.cluster - Adding discovered server host1:27017 to client view of cluster
06:39:09.079 [main] INFO org.mongodb.driver.cluster - Adding discovered server host2:27017 to client view of cluster
06:39:09.080 [main] INFO org.mongodb.driver.cluster - Adding discovered server host3:27017 to client view of cluster
06:39:09.083 [main] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=host3:27017, type=UNKNOWN, state=CONNECTING}, {address=host1:27017, type=UNKNOWN, state=CONNECTING}, {address=host2:27017, type=UNKNOWN, state=CONNECTING}]
Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.MongoClientSettings.getAutoEncryptionSettings()Lcom/mongodb/AutoEncryptionSettings;
    at com.mongodb.client.internal.MongoClientImpl.<init>(MongoClientImpl.java:67)
    at com.mongodb.client.internal.MongoClientImpl.<init>(MongoClientImpl.java:61)
    at com.mongodb.client.MongoClients.create(MongoClients.java:114)
    at com.mongodb.client.MongoClients.create(MongoClients.java:99)
    at com.mongodb.client.MongoClients.create(MongoClients.java:83)
    at com.mongodb.client.MongoClients.create(MongoClients.java:61)
    at com.mongo.demo.MongoDemo.main(MongoDemo.java:98)

下面的pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>mongo-test</groupId>
  <artifactId>mongo-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>mongo-test</name>
  <description>Mongo Test</description>
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>
  <dependencies>
      <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>3.11.0</version>
    </dependency>
            <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
           <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency> 
  </dependencies>
             <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

共有2个答案

鄢承运
2023-03-14

Spring Boot存在一些依赖性问题。我通过以下方法解决了这个问题:

  <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>bson</artifactId>
       <version>3.11.0</version>
  </dependency>
  <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver</artifactId>
      <version>3.11.0</version>
      <exclusions>
          <exclusion>
               <groupId>org.mongodb</groupId> 
               <artifactId>bson</artifactId>
           </exclusion>
      </exclusions>
  </dependency>
  <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongodb-driver-core</artifactId>
      <version>3.11.0</version>
  </dependency> 
戚翰飞
2023-03-14

你能试试这种依赖性吗

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

而不是

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>3.11.0</version>
</dependency>
 类似资料: