我正在使用 MongoBee 进行迁移,在mongoTemplate.insertAll(airports); 上遇到异常;
@ChangeLog(order = "001")
public class DbChangeLog001 {
@ChangeSet(order = "001", id = "seedProduct", author = "San")
public void seedProduct(MongoTemplate mongoTemplate) {
Product rome = new Product("Name" , "45.9","Desc");
Product paris = new Product("Name" , "45","Desc");
Product copenhagen = new Product("Name" , "45","Desc");
List<Product> airports = Arrays.asList(rome, paris, copenhagen);
mongoTemplate.insertAll(airports);
}
}
这个问题是由依赖引起的,我知道,但不知道如何解决这个问题
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'org.springdoc:springdoc-openapi-ui:1.4.3'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.cloud:spring-cloud-starter-config'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile "org.mongodb:mongo-java-driver:3.12.6"
compile 'org.javassist:javassist:3.18.2-GA' // workaround for ${javassist.version} placeholder issue*
compile('com.github.mongobee:mongobee:0.13') {
exclude group: 'org.mongodb'
}
}
从 Mongobee 文档中有一个已知问题
mongobee depends on mongo-java-driver. If your application has mongo-java-driver dependency too, there could be a library conflicts in some cases.
他们展示了一种解决方法
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.github.mongobee</groupId>
<artifactId>mongobee</artifactId>
<version>0.9</version>
<exclusions>
<exclusion>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
我已按照相同的指令从 Mongobee 中删除了 mongodb 依赖项,但给我带来了一个新错误
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.data.mongodb.core.MongoTemplate.lambda$insertDocumentList$16(MongoTemplate.java:1468)
The following method did not exist:
'com.mongodb.client.result.InsertManyResult com.mongodb.client.MongoCollection.insertMany(java.util.List)'
The method's class, com.mongodb.client.MongoCollection, is available from the following locations:
jar:file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.mongodb/mongo-java-driver/3.12.6/357010b0cdc0e6b2e1fc62e589be4a2c1e0050d1/mongo-java-driver-3.12.6.jar!/com/mongodb/client/MongoCollection.class
jar:file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.mongodb/mongodb-driver-sync/4.0.4/3f1538a82a5a8ba8d41f22dd61bb17b30f1a9407/mongodb-driver-sync-4.0.4.jar!/com/mongodb/client/MongoCollection.class
The class hierarchy was loaded from the following locations:
com.mongodb.client.MongoCollection: file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.mongodb/mongo-java-driver/3.12.6/357010b0cdc0e6b2e1fc62e589be4a2c1e0050d1/mongo-java-driver-3.12.6.jar