Bhupesh Singh Padiyar
6 min readOct 28, 2020

#Introduction:

In this post I’m going to setup REST API project with Spring Boot and this will use MongoDB database with Spring Data as persistence API.

Here I will make a Spring Boot REST CRUD Application which provides REST APIs for create user, retrieve user, update user and delete user.

#Prerequisites:

Before we start, make sure you have the following development environment.

  1. JDK 1.8 or above
  2. MongoDB 4.0.1
  3. Apache Maven 3.6
  4. IDE — STS (Spring Tool Suite preferred)

#Application Stracture:

There are four main entities involve in this application

  1. Database MongoDB NoSQL database to store and retrieve data.
  2. RestController Layer — To control requests and responses.
  3. Repository Layer — To store and retrieve data from database.
  4. Client — The client that want to access the API resource.

#Steps to create the application

There are following 3 ways to initialize spring boot project

We will use the third approach (Using Spring Initializr) here

Following are the steps to create the application

#Step 1: Initialize spring boot project & import to STS IDE

Go to https://start.spring.io/ & setup the maven project. Add following maven dependencies

  1. Web — Full-stack web development with Tomcat and Spring MVC.
  2. Spring Data MongoDB — Spring Boot connector for MongoDB
  3. Lombok — Java library tool that is used minimize boilerplate code and save development time

Generate, download, and import project to Spring Tool Suite (STS) IDE. Following is final project structure .

#Step 2: Create Database in MongoDB

I assume that you already have an accessible instance of MongoDB that you can connect to.

Let us start by creating a database “users_db” using the following command in the MongoDB shell, or through a database manager like MongoDB Compass, ROBO 3T or Studio 3T.

use users_db

#Step 3: Add a MongoDB Collection and Data

Let’s create a collection “users” and add some data to this collection.

db.createCollection("users");

Once the collection is created, we need to add some data! This collection will hold the user details i.e. user first name, user last name, user email and other data. A single document would be formatted as follows:

{
"firstName" : "Roger",
"lastName" : "Federer",
"email" : "roger@abc.com",
"age": "40",
"address": "Changi, Singapore",
"createdOn" : ISODate("2019-10-29T00:00:00Z"),
"updatedOn" : ISODate("2019-10-29T00:00:00Z")
}

We can add data to the collection with the following command

db.users.insertMany([
{
"firstName" : "Adam",
"lastName" : "Gilchrist",
"email" : "adam@xyz.com",
"age": "40",
"address": "Mont Kiara, Malaysia",
"createdOn" : ISODate("2019-10-29T00:00:00Z"),
"updatedOn" : ISODate("2019-10-29T00:00:00Z")
},
{
"firstName" : "Roger",
"lastName" : "Federer",
"email" : "roger@abc.com",
"age": "40",
"address": "Changi, Singapore",
"createdOn" : ISODate("2019-10-29T00:00:00Z"),
"updatedOn" : ISODate("2019-10-29T00:00:00Z")
}
]);

#Step 4: Configure MongoDB and server details in application.properties

Configure your MongoDB database details i.e database URL, db username, db password and application port in the application.properties file.

application.properties

#Server Port
server.port = 8888
#MongoDB database detailsspring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=users_db
#In case mongodb security is enabled,
#configure authentication DB & credentials as following
#spring.data.mongodb.authentication-database=[authentication_database]
#spring.data.mongodb.username=[username]
#spring.data.mongodb.password=[password]

#Step 5: Create Entity Class

Create an Entity class User.java to hold the user detail retrieved from DB and map the users table fields with this entity

Following diagram illustrates the mapping between the User.java and the user collection

User.java

package com.bhupeshpadiyar.sbrm.model;import java.util.Date;import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
/**
* @author Bhupesh Singh Padiyar
*
*/
@Getter
@Setter
@AllArgsConstructor
@Document(collection = "user")
public class User {
@Id
private String id;
private String firstName;
private String lastName;
private String email;
private Integer age;
private String address;
@CreatedDate
private Date createdOn;
@LastModifiedDate
private Date updatedOn;
}

#Step 6: Create Mongo Repository

UserRepository.java

package com.bhupeshpadiyar.sbrm.repository;import org.springframework.data.mongodb.repository.MongoRepository;import com.bhupeshpadiyar.sbrm.model.User;
/**
* @author Bhupesh Singh Padiyar
*
*/
public interface UserRepository extends MongoRepository<User, Long> { public User findById(String id);
public Long deleteById(String id);
}

#Step 7: Create Rest Controller and map all the API endpoints

UserController.java

package com.bhupeshpadiyar.sbrm.controller;import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bhupeshpadiyar.sbrm.model.User;
import com.bhupeshpadiyar.sbrm.repository.UserRepository;
/**
* @author Bhupesh Singh Padiyar
*
*/
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping(value = "/users")
public Map<String, Object> getAllUsers() {
List<User> users = userRepository.findAll();
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("users", users);
responseMap.put("status", 200);
responseMap.put("message", "Success");
return responseMap;
}
@GetMapping(value = "/user/{id}")
public Map<String, Object> findUser(@PathVariable String id) {
User user = userRepository.findById(id);
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("user", user);
responseMap.put("status", 200);
responseMap.put("message", "Success");
return responseMap;
}
@PostMapping(value = "/user")
public Map<String, Object> saveUser(@RequestBody User user) {
User savedUser = userRepository.save(user);
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("user", savedUser);
responseMap.put("status", 200);
responseMap.put("message", "Success");
return responseMap;
}
@PutMapping(value = "/user")
public Map<String, Object> updateUser(@RequestBody User user) {
User updatedUser = userRepository.save(user);
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("user", updatedUser);
responseMap.put("status", 200);
responseMap.put("message", "Success");
return responseMap;
}
@DeleteMapping(value = "/user/{id}")
public Map<String, Object> deleteUser(@PathVariable String id) {
Map<String, Object> responseMap = new HashMap<>(); try {
userRepository.deleteById(id);
responseMap.put("user", true);
responseMap.put("status", 200);
responseMap.put("message", "Success");
} catch (Exception e) {
responseMap.put("user", false);
responseMap.put("status", 500);
responseMap.put("message", "Error");
}
return responseMap;
}
}

#Step 8: Build &Run the application

Go to the project location in the terminal/command prompt & below command to build the application. It will create application .jar file inside the target directory.

mvn clean compile install

To run the java application use the following command

java -jar target/spring-boot-rest-mongodb-0.0.1-SNAPSHOT.jar

Alternatively, you can run the application without packing as following

mvn spring-boot:run

The application will start running at http://localhost:8888

#Testing the REST endpoints

After running app successfully, now we will test all the create, retrieve, update, delete resources. There are several tools, application & chrome plugins to test the REST services.

#Create user service:

Create User is a POST type service. Following is the services URL, request body syntex.

Service URL : http://localhost:8888/api/user

Request Type : POST

Request Body:

{      "firstName": "Adam",
"lastName": "Gilchrist",
"email": "adam@abc.com",
"age": 40,
"address": "Mont Kiara, Kuala Lumpur, Malaysia"
}

Response :

{
"message": "Success",
"user": {
"id": "5db79b324dec1703b65910ca",
"firstName": "Adam",
"lastName": "Gilchrist",
"email": "adam@abc.com",
"age": 40,
"address": "Mont Kiara, Kuala Lumpur, Malaysia",
"createdOn": "2019-10-29T01:51:46.889+0000",
"updatedOn": "2019-10-29T01:51:46.889+0000"
},
"status": 200
}

#List all users service:

Service URL : http://localhost:8888/api/users

Request Type : GET

Response :

{
"message": "Success",
"users": [
{
"id": "5db79b324dec1703b65910ca",
"firstName": "Adam",
"lastName": "Gilchrist",
"email": "adam@abc.com",
"age": 40,
"address": "Mont Kiara, Kuala Lumpur, Malaysia",
"createdOn": "2019-10-29T01:51:46.889+0000",
"updatedOn": "2019-10-29T01:51:46.889+0000"
},
{
"id": "5db79b794dec1703b65910cb",
"firstName" : "Roger",
"lastName" : "Federer",
"email" : "roger@abc.com",
"age": "40",
"address": "Changi, Singapore",
"createdOn": "2019-10-29T01:51:46.889+0000",
"updatedOn": "2019-10-29T01:51:46.889+0000"
}
],
"status": 200
}

#Get user by id service:

Service URL : http://localhost:8888/api/user/5db79b324dec1703b65910ca

Request Type : GET

Response :

{
"message": "Success",
"user": {
"id": "5db79b324dec1703b65910ca",
"firstName": "Adam",
"lastName": "Gilchrist",
"email": "adam@abc.com",
"age": 40,
"address": "Mont Kiara, Kuala Lumpur, Malaysia",
"createdOn": "2019-10-29T01:51:46.889+0000",
"updatedOn": "2019-10-29T01:51:46.889+0000"
},
"status": 200
}

#Update user service:

Service URL : http://localhost:8888/api/user/

Request Type : PUT

Request Body:

{    "firstName": "Adam",
"lastName": "Gilchrist",
"email": "adam@abc.com",
"age": 40,
"address": "New South Wales, Australia"
}

Response :

{
"message": "Success",
"user": {
"id": "5db79b324dec1703b65910ca",
"firstName": "Bhupesh Singh",
"lastName": "Padiyar",
"email": "bhupesh@xyz.com",
"age": 32,
"address": "New South Wales, Australia",
"createdOn": 2019-10-29T01:57:30.833+0000",
"updatedOn": "2019-10-29T01:57:30.833+0000"
},
"status": 200
}

#Delete user service:

Service URL : http://localhost:8888/api/user/5db79b324dec1703b65910ca

Request Type : DELETE

Response :

{
"message": "Success",
"user": true,
"status": 200
}

#Source Code:

You may Download the source code from the following GitHub link

https://github.com/bhupeshpadiyar/spring-boot-rest-mongodb

Thank You. Happy Learning!!!

Bhupesh Singh Padiyar
Bhupesh Singh Padiyar

Written by Bhupesh Singh Padiyar

Programmer | Full Stack Developer | Java Developer | Angular 5+ | NodeJS | MEAN Stack Developer

No responses yet