Reputation: 1124
I am trying to create a project in which I will be able to access mongo db through my controller and get forms from the user.
In my pom file I have declared to mongodb-driver, but when I try to `import com.mongodb.MongoClient I get an error - cannot resolve symbol mongodb.
I am trying to use the mongodb java driver with spring boot, since I have studied how to use in the M101J course.
The pom -here's the file -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>form-java-spring-freemarker</artifactId>
<name>form-java-spring-freemarker</name>
<description>form-java-spring-freemarker</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The other file - User.java -
package com.hellokoding.springboot;
import com.mongodb.MongoClient;
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
The file tree -
Upvotes: 1
Views: 6920
Reputation: 6450
As expected, I had also the issue because of conflict between newer version of mongodb driver 3.7+ and spring boot starter dependency, as I mentioned in comments. And solved it by adding instead of
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
newer version like:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
So, I highly recommend to use last versions of drivers and spring boot starter dependencies. And it should save you from such errors, I'm sure.
But also you can avoid it using older versions of mongodb java drivers, as you did it.
Upvotes: 2