使用 mongodb 为 spring 引导项目通过(maven 安装)构建 war 文件时出现异常

Exception while building a war file via (maven install) for a spring boot project with mongodb

我正在尝试使用 mongodb 构建一个 spring-boot 应用程序。我排除了 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-驱动程序依赖项替换为 mongodb-java-驱动程序依赖项

来解决
   <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.6.3</version>
    </dependency>
   

至于我遇到的情况,我没有添加我在应用程序中使用的依赖项。那是个错误。