DEV Community

Cover image for Building a REST API using Spring Boot and MongoDB.
Bamba Ndour
Bamba Ndour

Posted on • Updated on

Building a REST API using Spring Boot and MongoDB.

In this project we are going to learn how to build a simple crud backend rest api using Spring Boot and MongoDB.

Prerequisites :

In order to complete this tutorial, you should have knowledge of beginner or intermediate Java 8 programming skills, some familiarity with Spring Boot, and also you should have a general familiarity with the Windows command prompt.

Tools Used in this Project

Java 11
Spring Boot 2.6.6
Maven
Lombok
Intelli J
MongoDB 5.0
Postman

Install and Launch MongoDB

To start working with MongoDB, first, we have to install it on our local machines. Go to the MongoDB website and download the MongoDB installer from the downloads section.
Once the download is complete double click the file and follow the prompts to install Mongo. Mongo is most likely to be installed in the “C:\Program Files\MongoDB..” directory unless you specify a custom path.
Open a command prompt window and direct it to the bin folder inside the MongoDB folder path.

Let's start the server

Image description

Server Started:

Image description

  • Let's create a new Database and a new Collection

Image description

Spring Boot MongoDB APIs

We will have the following functionalities and Database interactions in our app.

  • Get all employees
  • Get a employee with ID
  • Save a employee
  • Update a employee
  • Delete a employee

Spring Boot Project Setup

We will make use of the Spring Initializr tool for quickly setting up the project. Don’t forget to add the dependencies Lombok, Spring Boot DevTools, Spring WEB & Spring Data MongoDB.

Image description

All Dependencies:

  • Spring Data MongoDB
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode
  • Spring Web
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode
  • Lombok
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>
Enter fullscreen mode Exit fullscreen mode
  • Spring Boot DevTools
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>
Enter fullscreen mode Exit fullscreen mode
  • Spring Boot Validator
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Configure Spring Datasource, MongoDB, App properties
Open src/main/resources/application.properties

  • For application.properties:
# MongoDB
spring.data.mongodb.host=[host]
spring.data.mongodb.port=[port]
spring.data.mongodb.username=[username]
spring.data.mongodb.password=[password]
spring.data.mongodb.database=[database_name]
spring.application.name=[Your Application Name]
spring.banner.charset=[UTF-8]
# logging
logging.level.org.springframework.data=debug
logging.level.=error
Enter fullscreen mode Exit fullscreen mode
  • For application.yml
spring:
  data:
     mongodb:
        authentication-database: admin
        port: 27017
        host: localhost
        database: EmployeeBackend
  application:
     name: employee-rest-api

  banner:
    charset: UTF-8
Enter fullscreen mode Exit fullscreen mode

Now you can see the project

Image description

Download All Packages

mvn package
Enter fullscreen mode Exit fullscreen mode

Running the Project and Testing with Postman
Now let’s test our API calls using postman. Right-click on the project and select Run as a Spring boot App.

Run Spring Boot application

mvn spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Image description

Postman

Testing With Postman

  • Get all employees

Image description

  • Save a new employee

Image description

  • Save another employee

Image description

  • Get all employees

Image description

  • Get employee by id

Image description

  • Get employee by a valid id

Image description

  • Edit employee by id

Image description

  • Delete employee by id

Image description

  • Get the number of employees that exist in the database

Image description

You can see now all employees in the database

Image description

The source code examples used in this project can be found on Github Repository:

Oldest comments (0)