在 Spring 项目中,不能直接使用 mongodb
的驱动。
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.8.2</version>
</dependency>
会检测到多个 jar 包从而导致错误:
***************************
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:
com.mongodb.client.internal.MongoClientImpl.createCluster(MongoClientImpl.java:225)
The following method did not exist:
'com.mongodb.internal.connection.Cluster com.mongodb.internal.connection.DefaultClusterFactory.createCluster(com.mongodb.connection.ClusterSettings, com.mongodb.connection.ServerSettings, com.mongodb.connection.ConnectionPoolSettings, com.mongodb.internal.connection.InternalConnectionPoolSettings, com.mongodb.connection.StreamFactory, com.mongodb.connection.StreamFactory, com.mongodb.MongoCredential, com.mongodb.event.CommandListener, java.lang.String, com.mongodb.MongoDriverInformation, java.util.List, com.mongodb.ServerApi)'
The calling method's class, com.mongodb.client.internal.MongoClientImpl, was loaded from the following location:
解决方案
使用 spring data 进行替换
<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>4.2.3</version>-->
<!-- </dependency>-->