Connecting Mongodb Atlas from Spring boot

VISHWANATH N P picture VISHWANATH N P · Mar 25, 2017 · Viewed 12.8k times · Source

I i am new to the Mongodb with Spring Boot.And i have MONGODB ATLAS URI Connection String

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

Then in my Spring Boot Application i set uri in application.properties like

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

This Repository intefface

public interface OrganizationRepository extends   MongoRepository<Organization, String> {   

}

when i Inject OrganizationRepository interface Its showing error like this

Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalArgumentException: The connection string contains invalid user information. If the username or password contains a colon (:) or an at-sign (@) then it must be urlencoded

And this my Document class

@Document(collection="Organization")
public class Organization {

    @Id
    String id;

    String orgName; 

    String orgAddress;

    String pinCode;

//getter 

//setter
}

This is my 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>

How we can fix this? Thanks

Answer

VISHWANATH N P picture VISHWANATH N P · Mar 27, 2017

I find the actual problem. In my pevious Atlas password contain @ (symbol) so its not supoort. I follwed document HERE

IMPORTANT If the password contains reserved URI characters, you must escape the characters per RFC 2396. For example, if your password is @bc123, you must escape the @ character when specifying the password in the connection string; i.e. %40bc123.