我我是新來使用Spring Boot.And MongoDB的我已經MongoDB的ATLAS URI連接字符串從春天啓動連接MongoDB的阿特拉斯
mongodb://userName:<PASSWORD>@icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/<DATABASE>?ssl=true&replicaSet=icarat-shard-0&authSource=admin
然後在我的春天啓動應用程序,我設置的URI application.properties像
spring.data.mongodb.uri: mongodb://userName:*****@icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/vehicleplatform?ssl=true&replicaSet=icarat-shard-0&authSource=admin
該存儲庫intefface
public interface OrganizationRepository extends MongoRepository<Organization, String> {
}
當我注入OrganizationRepository接口其顯示如下錯誤
無法實例化[com.mongodb.MongoClient]:工廠方法'mongo'拋出異常;嵌套異常是java.lang.IllegalArgumentException:連接字符串包含無效的用戶信息。如果用戶名或密碼包含一個冒號(:)或at符號(@),那麼它必須url編碼
這我的文檔類
@Document(collection="Organization")
public class Organization {
@Id
String id;
String orgName;
String orgAddress;
String pinCode;
//getter
//setter
}
這是我的pom.xml
<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>
<groupId>com.icarat</groupId>
<artifactId>vehicleplatform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>vehicleplatform</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
<!-- swagger2 dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId> springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我們如何解決這個問題?謝謝
改爲使用以下特定屬性作爲用戶名和密碼 spring.data.mongodb.username = spring.data.mongodb.password = – Fahad