Spring Boot and MongoDB REST API CRUD Tutorial

In this tutorial, we will learn creating a REST API using Spring Boot and MongoDB. We will perform the basic database operations like Create, Read, Update and Delete. So we will create a backend application using Spring boot and we will provide a full CRUD support using MongoDB. So let’s begin..





When we are working with Spring boot and MongoDB, we cant use @GeneratedValue annotation in our entity class as it does not support. But somehow we need to use auto generated field so that we can keep track of employee records and perform the Read, Update and Delete operations. In this tutorial, we will also learn generating auto id for the each document.

Setup MongoDB Database


In order to use MongoDB i will make use of https://mlab.com/ website which provide a MongoDB database service online. So we can connect to their website and use MongoDB. Watch the below video to setup the MongoDB database in mLab.

NOTE: If you don’t want to use mLab and if you want to use MongoDB locally, you can use that as well.




Tools and Technologies Used:


  • Spring Boot – 2.1.2.RELEASE
  • Spring Framework – 5.1.4.RELEASE
  • Spring Data – 2.1.4.RELEASE
  • MongoDB – 3.8.2
  • JDK – 1.8 or later
  • IDE – Eclipse Oxygen

Now let’s look at the development steps,

Development Steps


  1. Create Spring boot application
  2. Update the pom.xml file
  3. Project structure
  4. Configure MongoDB in mLab
  5. Create model class
  6. Create Spring data repository
  7. Create controller
  8. Run application
  9. Test REST API’s using Rest client

Let’s look at the steps one by one

1. Create Spring boot application


There are many ways to create Spring boot application, you can use any one of the way to create spring boot application and open it in Eclipse




2. Update the pom.xml file


We will add two main dependencies spring-boot-starter-data-mongodb and spring-boot-starter-web in pom.xml

3. Project structure


Below is the Spring boot project structure for your reference..

 

 

 

 

 

 

 

 

4. Configure MongoDB in mLab


Spring boot tries to connect mongoDB by reading the pom.xml dependency spring-boot-starter-data-mongodbwhich we have added. All we need to do is add the database url in the application.properties file.

As per my mLab configuration mongodb running on the port 21694, username is admin and password is admin123.
NOTE: Make sure to use your configuration, do not use mine.
If you have installed mongodb locally then add this,

and make sure to create employees database using the following command:

5. Create model classes


We are going to create two model classes DatabaseSequence which creates a auto generated value for the id column.




Now, let’s create EmployeeModel class to store the employee object

6. Create spring data repository


Next, we need to create repository EmployeeDAO to access the data from database

Now let’s create SequenceGeneratorService to create the auto generated value for id property

7. Create controller


Next let’s create a rest end points using @GetMapping, @PostMapping, @PutMapping, @DeleteMapping. We have annotated with @RestController to create rest end points. Also notice that we are calling generateSequence() while creating employee record.




8. Run the application


Since this is a Spring boot application, you will get a main method SpringBootMongodbRestApiApplication. You can run this application just like normal java class.

9. Test app using REST client





create a employee record
read all the employeesread a single employeeupdate the employee recorddelete a employee record



About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

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