当前位置: 首页 > 知识库问答 >
问题:

spring和mongodb集成问题-纠正应用程序的类路径,使其包含一个单一的兼容版本

农飞尘
2023-03-14

我在spring、flowable和MongoDB上工作。

这是我的战俘

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-engine</artifactId>
            <version>6.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-process-engine-mongodb</artifactId>
            <version>6.4.0.alpha1</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

运行时,它抛出一个错误

试图调用不存在的方法。尝试是从以下位置进行的:

com.mongodb.MongoClientOptions.<init>(MongoClientOptions.java:149)

The following method did not exist:

    'com.mongodb.connection.ConnectionPoolSettings$Builder com.mongodb.connection.ConnectionPoolSettings$Builder.maxWaitQueueSize(int)'

The method's class, com.mongodb.connection.ConnectionPoolSettings$Builder, is available from the following locations:

    jar:file:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.1.2/mongodb-driver-core-4.1.2.jar!/com/mongodb/connection/ConnectionPoolSettings$Builder.class

The class hierarchy was loaded from the following locations:

    com.mongodb.connection.ConnectionPoolSettings.Builder: file:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.1.2/mongodb-driver-core-4.1.2.jar
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.ConnectionPoolSettings$Builder

首先,我不知道maxWaitQueueSize函数在哪里使用,因为这个函数存在于mongodb driver 3.6.0-beta1中,而不存在于4.x.x版本中,

我试着加了pom

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.6.0-beta1</version>
    </dependency>

然后运行带有新错误的pom

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.mongodb.MongoClientOptions.<init>(MongoClientOptions.java:149)

The following method did not exist:

    'com.mongodb.connection.ConnectionPoolSettings$Builder com.mongodb.connection.ConnectionPoolSettings$Builder.maxWaitQueueSize(int)'

The method's class, com.mongodb.connection.ConnectionPoolSettings$Builder, is available from the following locations:

    jar:file:/Users/abc/.m2/repository/org/mongodb/mongodb-driver-core/4.1.2/mongodb-driver-core-4.1.2.jar!/com/mongodb/connection/ConnectionPoolSettings$Builder.class
    jar:file:/Users/abc/.m2/repository/org/mongodb/mongo-java-driver/3.6.0-beta1/mongo-java-driver-3.6.0-beta1.jar!/com/mongodb/connection/ConnectionPoolSettings$Builder.class

The class hierarchy was loaded from the following locations:

    com.mongodb.connection.ConnectionPoolSettings.Builder: file:/Users/xisizhe/.m2/repository/org/mongodb/mongodb-driver-core/4.1.2/mongodb-driver-core-4.1.2.jar

我试图删除mongodb-driver-core-4.1.2.jar,但再次运行mongodb-driver-core-4.1.2.jar时,将再次自动下载mongodb-driver-core-4.1.2.jar,项目将继续使用4.1.2版本,而不是pom.xml中配置的mongo-java-driver-3.6.0

[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.3.3.RELEASE:run (default-cli) > test-compile @ demo >>>
Downloading from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver/3.8.2/mongodb-driver-3.8.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver/3.8.2/mongodb-driver-3.8.2.pom (1.8 kB at 1.0 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/mongodb/bson/4.0.5/bson-4.0.5.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/mongodb/bson/4.0.5/bson-4.0.5.pom (1.3 kB at 2.7 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.pom (3.1 kB at 7.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver/3.8.2/mongodb-driver-3.8.2.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/mongodb/bson/4.0.5/bson-4.0.5.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver/3.8.2/mongodb-driver-3.8.2.jar (320 kB at 339 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/mongodb/bson/4.0.5/bson-4.0.5.jar (492 kB at 178 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/mongodb/mongodb-driver-core/4.0.5/mongodb-driver-core-4.0.5.jar (1.6 MB at 550 kB/s)

最让我困惑的是,我没有在pom.xml中配置任何mongodb-driver-core-4.1.2,它来自哪里?

有办法解决这个吗?

共有1个答案

万阳嘉
2023-03-14

我不能用你的pom复制你的问题,我得到了以下内容:

Factory method 'mongo' threw exception; nested exception is java.lang.NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory

无论如何,问题很可能是MongoAutoConfigurationflowable-process-engine-MongoDB库所依赖的MongoDB-Driver版本不兼容,当类路径中有可用的MongoAutoConfiguration时,MongoAutoConfiguration就会出现这种情况。如果运行MVN Dependency:Tree,您将看到以下内容:

[INFO] \- org.flowable:flowable-process-engine-mongodb:jar:6.4.0.alpha1:compile
[INFO]    +- org.mongodb:mongodb-driver:jar:3.8.2:compile
[INFO]    |  +- org.mongodb:bson:jar:4.1.2:compile
[INFO]    |  \- org.mongodb:mongodb-driver-core:jar:4.1.2:compile

简单地将这个jar从这个依赖项中排除,就可以运行应用程序,如下所示:

xml prettyprint-override"><dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-process-engine-mongodb</artifactId>
    <version>6.4.0.alpha1</version>
    <exclusions>
        <exclusion>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
        </exclusion>
    </exclusions>
</dependency>

但是您可能希望在应用程序中配置mongoclient,在这种情况下,您将需要替换排除的JAR:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
</dependency>

它是mongoDB的同步驱动程序,但是您也可以使用Async驱动程序。您不必提供版本,因为它是托管依赖项。

@SpringBootApplication(exclude = MongoAutoConfiguration.class)
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
 类似资料: