'Connecting Mongodb Atlas from Spring boot

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



Solution 1:[1]

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.

Solution 2:[2]

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

The problem is crystal clear, if you are trying to connect with MongoDB Atlas with SpringBoot, your password must be URL Encoded which is more secure.

Here you can see how to URL encode password in MongoDB Atlas.

Solution 3:[3]

You can use the Mongodb SRV record to define all nodes associated to the cluster

  # Mongo Connection
  spring.data.mongodb.uri=mongodb+srv://myuser:[email protected]/mydb?retryWrites=true&w=majority

where

  • myuser: Mongodb user
  • mypassword: password of the user above
  • mycluster.azure.mongodb.net: host of your cluster
  • mydb: Mongodb database of your application

It is important to use the correct SRV assigned to you by Atlas (in your example you have combined all three shards), which you can get in the "Connect Your Application" screen, together with a code example (if needed).

Copy the connection string, replace password and dbname, set this in the SpringBoot property file.

Solution 4:[4]

I think best escape is to follow this pattern:

spring.data.mongodb.authentication-database= # Authentication database name. spring.data.mongodb.database=db_name. spring.data.mongodb.host=127.0.0.1 spring.data.mongodb.password=passwrd spring.data.mongodb.port=27017 spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories. spring.data.mongodb.username= # Login user of the mongo server.

Solution 5:[5]

In my case I'm added {} surrounded with the documentDb url

Solution 6:[6]

I faced same issue when connecting to Mongo DB Atlas from spring boot: 'If the username or password contains a colon (:) or an at-sign (@) then it must be urlencoded'

This occurs if the password contains above mentioned special characters, I was able to solve the issue by using the encoded password.

Go to https://www.urlencoder.io/ -> type the password to be encoded, Example: 'sample@password' , encoded string will be 'sample%40password'. Now use this in your connection uri by replacing the password

the password value will look something like


mongodb://userName:sample%40password@icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/testdb?ssl=true&replicaSet=icarat-shard-0&authSource=admin


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VISHWANATH N P
Solution 2 Mehraj Malik
Solution 3 Beppe C
Solution 4
Solution 5 jerald jacob
Solution 6 Divya