While trying to run Spring-boot application (v2.1.0) getting below error:

Description:

An attempt was made to call the method org.springframework.data.mongodb.core.MongoTemplate.(Lcom/mongodb/Mongo;Ljava/lang/String;)V but it does not exist. Its class, org.springframework.data.mongodb.core.MongoTemplate, is available from the following locations:

file:/C:/Users/npatil/.m2/repository/org/springframework/data/spring-data-mongodb/2.1.2.RELEASE/spring-data-mongodb-2.1.2.RELEASE.jar!/org/springframework/data/mongodb/core/MongoTemplate.class

It was loaded from the following location:

file:/C:/Users/npatil/.m2/repository/org/springframework/data/spring-data-mongodb/2.1.2.RELEASE/spring-data-mongodb-2.1.2.RELEASE.jar

Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.mongodb.core.MongoTemplate

Below is a snnipet from my pom:

<dependencies>
.
.
  <dependency>
    <groupId>com.github.mongobee</groupId>
    <artifactId>mongobee</artifactId>
    <version>0.13</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>2.1.2.RELEASE</version>
  </dependency>
.
.
</dependencies>

Version of some of the jars that could help are:
spring-data-mongodb : 2.1.2.RELEASE
spring-web : 5.1.2.RELEASE
mongo-java-driver : 3.8.2.RELEASE

Deleted .m2 and did mvn clean install, but even that did not resolve the issue. Any help would be greatly appreciated.

2

There are 2 best solutions below

0
TheSprinter On

Looks like jar is corrupted or missing, check all spring related jar are 4.x or higher version if that doent work then:
Mongobee depends on Spring 4.x jars which may conflicts with Spring boot 2.x

try the below way

  @Bean
  public Mongobee mongobee(){
    Mongobee mongobee = new Mongobee("mongodb://localhost:27017/seed");
    mongobee.setChangeLogsScanPackage(InitialData.class.getPackageName());
            mongobee.setMongoTemplate(template);

    return mongobee;
  }
0
Stéphane GRILLON On

You need change Mongobee by Mongock if you use Springboot 2. The syntax is almost the same because the Mongock project is the continuation of the Mongobee project.

Springboot:

@Bean
public SpringBootMongock mongock(ApplicationContext springContext, MongoClient mongoClient) {
  return (SpringBootMongock) new SpringBootMongockBuilder(mongoClient, "yourDbName", "com.package.to.be.scanned.for.changesets")
      .setApplicationContext(springContext) 
      .setLockQuickConfig()
      .build();
}

Spring:

@Bean
public SpringMongock mongock() {
  MongoClient mongoclient = new MongoClient(new MongoClientURI("yourDbName", yourMongoClientBuilder));
  return new SpringMongockBuilder(mongoclient, "yourDbName", "com.package.to.be.scanned.for.changesets")
      .setLockQuickConfig()
      .build();
}