Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 385
VISHWANATH N P Avatar asked Mar 25 '17 10:03

VISHWANATH N P


3 Answers

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.

like image 56
VISHWANATH N P Avatar answered Oct 17 '22 20:10

VISHWANATH N P


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.

like image 4
Mehraj Malik Avatar answered Oct 17 '22 22:10

Mehraj Malik


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.

like image 3
Beppe C Avatar answered Oct 17 '22 20:10

Beppe C