Spring boot and MongoDB REST API CRUD Tutorial [Updated 2021]





Hey, guys in this, we will learn connecting the spring boot REST API that connects to the MongoDB database and we will perform the basic database operations like Create, Read, Update and Delete.

Development steps


Following are the development steps to create a spring boot application and connect to the mongodb database

Create a spring boot project using Spring Initializr


Create a spring boot using spring initializr and add the following the dependencies

  • Web
  • Spring Data MongoDB
  • Devtools
  • Lombok
  • Validation

Setting up the MongoDB database


Download and install the MongoDB database locally on your machine from the following URL

  • https://www.mongodb.com/try/download/community





Next, in order to connect to the MongoDB database, we need a GUI tool that will help us to connect/maintain the database. so let’s download and install the robo3t from the following URL

  • https://robomongo.org/download

Setup the connection string


Next, open a project in eclipse IDE or any other IDE with which you are comfortable. Inside the resources folder, open application.properties and add the following connection string

Here, todo-manager-api is the database name

Create an Entity class


Create a new entity class TodoDTO.java inside the package in.bushansirgur.springbootmongodb.model and add the following code

TodoDTO.java

Create a Repository interface


Create an interface TodoRepository.java inside the package in.bushansirgur.springbootmongodb.repository that extends the JpaRepository.




TodoRepository.java 

Setup the configuration for validation


So we have to setup some validations for our entity class so let’s create a class ValidationConfig.java that configures the validation. Create a package in.bushansirgur.springbootmongodb.config and add the following code inside the configuration class

ValidationConfig.java

Create a custom exception handler


We need to create a custom exception handler to catch the business exceptions, so let’s create one for our application. Create a package in.bushansirgur.springbootmongodb.exception and inside this create a class TodoCollectionException.java, and add the following code




TodoCollectionException.java

Create a Service and Service implementation


Now let’s create service interface TodoService.java inside the package in.bushansirgur.springbootmongodb.service and add the following code

TodoService.java

Now let’s create an implementation class TodoServiceImpl.java inside the same package and add the following code

Create a controller and create the rest end points


Now let’s create a controller inside the package in.bushansirgur.springbootmongodb.controller and add the following code




TodoController.java

Run the main class


When we create a new spring boot project it automatically creates a main class for us, inside the package in.bushansirgur.springbootmongodb with class name SpringbootmongodbApplication.java, open it and run the file

SpringbootmongodbApplication.java

 

Download the project from Github


You can clone the entire app from the following Github repository



clone the project Spring boot and mongodb REST API 

 

Watch the full video tutorial


You can watch the entire tutorial in the following link

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 *