I have java application based on mongo server 4.2 and component versions are as follows
spring-boot 2.2.5.RELEASE
spring-data-mongodb2.2.5.RELEASE
mongo-java-driver3.12.1
I updated spring boot to 2.5.8 hence made few changes to update dependencies as follow
spring-boot2.5.8
spring-starter-parent2.5.8
spring-data-mongodb 3.2.3
mongodb-driver-core 4.2.3
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.8</version>
</dependency>
As I used newly added
**import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;**
i.e mordern API using mongo-java driver 3.12.1 I updated to mongodb-driver-sync 4.2.3
I resolved all compilation errors but on deployment got errors like method not found for getSrvHost() on further debugging found that old dependency of mongo-java-driver 3.3.0 was already present in project
I excluded that dependency and above issue got resolved now no old dependency is present but still getting following error without using legacy API approach
java.lang.NoClassDefFoundError: com/mongodb/MongoClient
Caused by: java.lang.NoClassDefFoundError: com/mongodb/MongoClient
Completely stuck at this point in driver and spring upgrade process, without using com.mongodb.MongoClient in entire project it is throwing error on deployment.
It's recommended use spring-boot-starter-parent as <parent> to use compatible versions.
Just add the spring-boot-starter-data-mongodb dependency and it has all the necessary MongoDB drivers.
Remove from pom.xml: spring-data-mongodb, mongodb-driver-core, spring-starter-parent
Note: Always use the last version, since 2.5.8 has 3 vulnerabilities
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/>
</parent>
...
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>