Spring boot 2.0.5.RELEASE and mongo 4.0 connection issues

I think this article on Overriding dependency versions with Spring Boot will help you. For reactor replace with mongodb. The MongoDB drivers themselves are pretty-much backwards compatible so you shouldn't have any big problems forcing a newer version.


Look at the method not found log presented by spring boot.

com.mongodb.connection.DefaultClusterFactory.createCluster( Lcom/mongodb/connection/ClusterSettings; Lcom/mongodb/connection/ServerSettings; Lcom/mongodb/connection/ConnectionPoolSettings; Lcom/mongodb/connection/StreamFactory; Lcom/mongodb/connection/StreamFactory; Ljava/util/List;Lcom/mongodb/event/CommandListener; Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation; Ljava/util/List;)Lcom/mongodb/connection/Cluster;

Note the 7th argument Lcom/mongodb/client/MongoDriverInformation. The MongoDriverInformation class has been moved from com/mongodb/client to com/mongodb in 3.7 beyond versions.

So it appears that you have both 3.6.4 and 3.8.0 jars on your classpath.

So try to clean your classpath and <mongodb.version>3.8.0</mongodb.version> is the correct way of overriding mongodb dependencies until spring boot release.

Btw there is pending 2.1.0 boot release which will update the mongodb dependency to 3.8.2 Right now it is a release candidate stage.


I also met this problem. when I first added pom:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.4.3</version>
    </dependency>

After compiling the project, it occured:

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call the method com.mongodb.connection.DefaultClusterFactory.createCluster(Lcom/mongodb/connection/ClusterSettings;Lcom/mongodb/connection/ServerSettings;Lcom/mongodb/connection/ConnectionPoolSettings;Lcom/mongodb/connection/StreamFactory;Lcom/mongodb/connection/StreamFactory;Ljava/util/List;Lcom/mongodb/event/CommandListener;Ljava/lang/String;Lcom/mongodb/client/MongoDriverInformation;Ljava/util/List;)Lcom/mongodb/connection/Cluster; but it does not exist. Its class, com.mongodb.connection.DefaultClusterFactory, is available from the following locations:

jar:file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar!/com/mongodb/connection/DefaultClusterFactory.class

It was loaded from the following location:

file:/C:/Users/winUser/.m2/repository/org/mongodb/mongodb-driver-core/3.8.0/mongodb-driver-core-3.8.0.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.mongodb.connection.DefaultClusterFactory

Then I corrected the pom, I solved this mistake, but I saw another mistake: This is the new pom, and i think its just owing to mongo_version.

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.8.0</version>
    </dependency>

This is the new warning:

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongodb-driver-core-3.8.2.jar:na]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_191]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_191]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_191]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_191]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_191]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_191]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64) ~[mongodb-driver-core-3.8.2.jar:na]
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.8.2.jar:na]
... 3 common frames omitted

So this problem is so easy and network has many methods to solve it, you just add one annotation to let spring boot ignore default mongo_properties:

@SpringBootApplication(exclude = MongoAutoConfiguration.class)

Then done!