[英]Cannot find the MongoClient class in the com.mongodb.client package provided by SpringBoot
For few hours I am trying to find the solution for this issue but could not resolve it for my own.几个小时以来,我一直在努力寻找解决此问题的方法,但我自己无法解决。 The problem is that my Spring Boot Application needs a MongoConfiguration class which should create a MongoClient object for further use to create a MongoTemplate object and it seems like I do not have a required dependency.问题是我的 Spring 引导应用程序需要一个 MongoConfiguration class,它应该创建一个 MongoClient object 以进一步用于创建 MongoTemplate object,而且我似乎没有必需的依赖项。 I am curious if Spring does not provide it automaticly or I am doing something wrong, but every guide about how to create the mongoTemplate Bean says it is so easy - "just make a config class with the MongoClient object, blablabla, it is ready."我很好奇 Spring 是否没有自动提供它或者我做错了什么,但是关于如何创建 mongoTemplate Bean 的每个指南都说它非常简单 - “只需使用 MongoClient object 进行配置 class,blablabla,它已准备就绪。 “ but I can not even use this class.但我什至不能使用这个类。
The Config class looks like this ( the showed imports are not recognized by the compiler ): Config class 看起来像这样(编译器无法识别显示的导入):
...
import com.mongodb.MongoClient;
import com.mongodb.client.*;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Override
protected String getDatabaseName() {
return "pizzeria";
}
@Override
public MongoClient mongoClient() {
ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/pizzeria");
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.build();
return MongoClients.create(mongoClientSettings);
}
@Override
public Collection getMappingBasePackages() {
return Collections.singleton("pl.mvasio.pizzeria.data");
}
}
And this is my pom.xml:这是我的 pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.mvasio</groupId>
<artifactId>pizzeria</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pizzeria</name>
<description>Design and order your favorite pizza</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.mongodb</groupId>-->
<!-- <artifactId>mongo-java-driver</artifactId>-->
<!-- <version>3.4.0</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.mongodb</groupId>-->
<!-- <artifactId>mongodb-driver-core</artifactId>-->
<!-- <version>3.4.0</version>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
As you can see i have tried to add additional dependency - mongo-java-driver and it helped with this issue but unfortunately provides another one so i need to resolve this.如您所见,我已尝试添加额外的依赖项 - mongo-java-driver,它有助于解决此问题,但不幸的是提供了另一个依赖项,因此我需要解决此问题。 I will be really grateful for any advice.如果有任何建议,我将不胜感激。
Thanks in advance.提前致谢。
You can conect to mongo directly trough the application.properties您可以直接通过 application.properties 连接到 mongo
spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
using this dependency:使用此依赖项:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
also you can:你也可以:
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=<username specified on MONGO_INITDB_ROOT_USERNAME>
spring.data.mongodb.password=<password specified on MONGO_INITDB_ROOT_PASSWORD>
spring.data.mongodb.database=<the db you want to use>
depending on your driver.取决于你的司机。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.