2

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.

Nitin1706
  • 621
  • 1
  • 11
  • 21
  • 1
    Can you please elaborate on how you upgrade the driver version? Solely upgrading that should not affect the dependency versions of Spring Framework but a mismatch in those seems to be the problem here. Also, note that upgrading to a new major version of a driver outside of a Spring Boot upgrade (2.0 is pretty dated and not supported anymore) may cause issues in itself as the new major version might contain breaking changes. That’s nothing Spring Boot can do anything about. Be advised to upgrade Boot itself to a recent version to get controlled updates to the MongoDB driver as well. – Oliver Drotbohm Oct 10 '20 at 11:08
  • @OliverDrotbohm: I have updated the post with the snippets of dependencies. – Nitin1706 Oct 12 '20 at 13:38
  • this was another issue I found when I tried using the mongo-driver-legacy instead of mongo-driver-sync. Missing class: com.mongodb.util.JSONParseException – Nitin1706 Oct 12 '20 at 21:43

1 Answers1

0

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>
shubham kumar
  • 271
  • 2
  • 6
  • As mentioned in question, mongo-java-driver version 3.11.2 is working as expected with springboot 2.0.4. Issue is about the compatibility of mongo-java-driver version 4.x.x with springboot 2.0.x version. – Nitin1706 Nov 04 '20 at 23:04
  • make sure you are not missing spring-data-commons-core jar. if yes then mark useful – shubham kumar Nov 05 '20 at 12:18
  • facing the same issue even with spring-data-commons-core jar. Finally upgraded the springboot version to make sure all are at same version. – Nitin1706 Nov 19 '20 at 23:01