Mongo Java Driver version mismatch with spring framework

4.2k Views Asked by At

I have springboot (2.0.4) application with Mongo Java driver version 3.11.2. When upgrading the application for mongo java driver to version 4.1.0 to use IAM authentication feature of new mongo java driver, the changes are breaking with overall spring framework.

MongoTemplate Bean code:

@Bean
public MongoClient mongoClient() {
    ConnectionString connectionString = new ConnectionString(
                    ("mongodb://connectionString:goesHere"));
    MongoClient mongoClient = MongoClients.create(connectionString);
    return mongoClient;
}
@Bean
public MongoTemplate mongoTemplate(MongoClient mongoClient) {
    return new MongoTemplate(mongoClient, mongoDB);
}

Dependencies:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>bson</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-core</artifactId>
        <version>4.1.0</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver</artifactId>
            </exclusion>
        </exclusions>
    </dependecy>

Deployment error with Mongo Java Driver 4.1.0:

Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoSuchMethodError: org.springframework.util.Assert.noNullElements(Ljava/util/Collection;Ljava/lang/String;)V

When looking into this issue, found that I need to add sping-core (5.2.5 or later) for this, that in turn is asking for other spring dependencies to be on same version.

This whole defeats the goal of having spring boot for ease of dependency management. It feels like spring ecosystem has become so complex, that upgrading mongo-java-driver would need the un-necessary work of upgrading the whole application to newer version of springboot which are not backward compatible and will break the application build. Any suggestions to get rid of this issue.

1

There are 1 best solutions below

3
On

i'm using these pom you can give it a try for mongo-3.8.2 and spring-data :1.10.0 . Make sure you are not missing "spring-data-commons-core jar"

             <properties>
                 <mongo.java.driver.version>3.8.2</mongo.java.driver.version>
                  <spring.data.version>1.10.0.RELEASE</spring.data.version>
            </properties>

             <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
                <version>${mongo.java.driver.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons-core</artifactId>
                <version>1.4.1.RELEASE</version>
            </dependency>
             <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-mongodb</artifactId>
                <version>${spring.data.version}</version>
            </dependency>