我正在尝试将 mongodb 从 3.x 升级到 4.x,因为我们需要在 mongodb 中支持事务,并且还需要将 fongo 用于 junits。我已经修改了以下给定依赖项的版本:
<spring-data-mongodb-version>2.1.0.RELEASE</spring-data-mongodb-version>
<spring-data-commons-version>2.1.0.RELEASE</spring-data-commons-version>
<spring-framework-version>5.0.8.RELEASE</spring-framework-version>
<mongo-java-driver>3.2.8</mongo-java-driver>
fongo -> 2.1.0
在给定的代码下面添加到TestConfig 类:
private static final MongoClient createNewMongo() {
Fongo fongo = new Fongo("fongo");
MongoClient mongo = fongo.getMongo();
return mongo;
}
@Override
@Bean
public MongoTemplate mongoTemplate() throws Exception {
Fongo fongo = new Fongo("fongo");
MongoClient mongo = fongo.getMongo();
return new MongoTemplate(mongo, DATABASE_NAME);
}
@Bean
public MongoClient mongoClient() {
MongoCredential mongoCredential = MongoCredential
.createMongoCRCredential("ext-catalog", "ext-catalog",
"ext-catalog".toCharArray());
List<MongoCredential> credentialsList = new ArrayList<>();
credentialsList.add(mongoCredential);
ServerAddress serverAddress = null;
try {
serverAddress = new ServerAddress("localhost" , 27017 );
} catch (Exception e) {
e.printStackTrace();
}
return new MongoClient(serverAddress, credentialsList);
}
低于给定错误:
java.lang.NoClassDefFoundError: com/mongodb/FongoMongoDatabase
at com.mongodb.MockMongoClient.getDatabase(MockMongoClient.java:88) ~[fongo-2.1.0.jar:na]
at org.springframework.data.mongodb.core.SimpleMongoDbFactory.doGetMongoDatabase(SimpleMongoDbFactory.java:105) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoDbFactorySupport.getDb(MongoDbFactorySupport.java:107) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoDbFactorySupport.getDb(MongoDbFactorySupport.java:95) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.MongoDatabaseUtils.doGetMongoDatabase(MongoDatabaseUtils.java:108) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.MongoDatabaseUtils.getDatabase(MongoDatabaseUtils.java:68) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doGetDatabase(MongoTemplate.java:2235) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:534) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.DefaultIndexOperations.execute(DefaultIndexOperations.java:218) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.DefaultIndexOperations.ensureIndex(DefaultIndexOperations.java:121) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:145) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
有什么办法可以解决吗?我发现一个链接说这两个不兼容。