mongodb - 通过(maven install)为带有mongodb的spring boot项目构建war文件时出现异常
问题描述
我正在尝试spring-boot
使用mongodb
. 我排除了spring-boot-starter-data-mongodb
依赖并使用mongodb-driver
3.12.5。
我的 application.properties 文件仅包含spring.data.mongodb.uri=connection _string
.
使用上述配置,我无法构建要在服务器中部署的 war 文件。它给出了以下错误。
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
Caused by: java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory
Caused by: java.lang.ClassNotFoundException: com.mongodb.connection.DefaultClusterFactory
请帮忙,谢谢。
解决方案
通过将 mongodb-driver 依赖项替换为 mongodb-java-driver 依赖项解决了这个问题
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.6.3</version>
</dependency>
至于我面临的情况,我没有添加我在应用程序中使用的依赖项。那是错误。