Spring Boot application with MongoDB Atlas Cloud persistence #1 part

NoSQL databases and microservices are common components of modern applications. One of the most popular NoSQL database is MongoDB and the most wide use framework for microservices is Spring Boot.

This post will guide you through the creation of simple microservice in Spring Boot what use MongoDB Atlas as repository.

Note: MongoDB Atlas is the global cloud database service for modern applications.
Note: CRUD is commonly used shortcut for basic DB operations Create Read Update Delete

Prerequisites 

  1. Created free account on MongoDb Atlas
  2. Java 13 installed 
  3. Apache Maven installed
  4. Some IDE (e.g. IntelliJ IDEA)

Creation of Spring Boot app

We have to create Spring Boot application at the beginning.

  1. Create Spring Boot project by online tool Spring Initializr
  2. Fill form
    1. Project: Maven Project
    2. Language: Java
    3. Spring Boot: 2.2.1
    4. Project Metadata / Group: org.korecky
    5. Project Metadata / Artifact: spring-mongodb-demo
    6. Project Metadata / Options / Packaging: JAR
    7. Project Metadata / Options / Java: 13
    8. Dependencies: 
      1. Spring Data MongoDB
  3. Click button Generate

Page generated ZIP file with empty project structure. You can open this project in your IDE.

You can compile the project in your IDE or you can do it by maven by running this command in your project directory.

mvn clean install

You have to see “BUILD SUCCESS” message at the end of output. You will see also some errors about MongoSocketOpenException, but don’t worry, we will fix it ASAP.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.186 s
[INFO] Finished at: 2019-11-08T20:34:31+01:00
[INFO] ------------------------------------------------------------------------

Connect to MongoDB Atlas Cloud

We have to little bit prepare MongoDB Atlas Cloud first. We needs user for our application.

  1. Sign-in to MongoDB Atlas Cloud
  2. Go to a menu SECURITY / Database Access in the left navigation
  3. Click on button ADD NEW USER
  4. Fill new username and password
  5. Select Read and write to any database option
  6. Then click Add User
  7. Done, we have a new user

When we have created user we can connect our application.

  1. Open your project and paste text below to the configuration file src/main/resources/application.properties in your project
spring.data.mongodb.uri=
spring.data.mongodb.database=test

spring.data.mongodb.uri is an attribute for connection string to MongoDB Atlas
spring.data.mongodb.database is an attribute for database name. If you use free MongoDB Atlas cloud default DB name is test

Let’s find  connection string in MongoDB Atlas administration page.

  1. Sign-in to MongoDB Atlas Cloud (if you are logged out)
  2. Go to ATLAS / Clusters in the left hand navigation
  3. Click button Connect
  4. Select Connect your application in the next screen
  5. Set form
    1. DRIVER: Java
    2. VERSION: 3.6 or later
  6. Then click Copy button on Connection String Only tab
  7. We can set spring.data.mongodb.uri in src/main/resources/application.properties file 
  8. Modify username and password by your values


We can compile the project again there is no MongoSocketOpenException error and build is successful.

We will create simple endpoint with CRUD operations in the next part of the series.

6 thoughts to “Spring Boot application with MongoDB Atlas Cloud persistence #1 part”

  1. Anxiety medication that does not cause fat gain can include Effexor, Paxil and BuSpar.

    You can find no known drug interactions in between macrodantin and the birth control pill.

    does tretinoin work

    Common starting dosage: 1 mg daily in the morning or evening.

    Most people I talk to Miss and brush it off since nothing.

    fktrpr94f
    The population-based study has shown that the two prior severe infections and autoimmune diseases raises the risk of subsequently creating depression.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.