Building a REST API using Spring Boot and MongoDB.
Published: 2022-06-20 19:10:35

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfy04n8b85vwgrqxg6il.PNG)

### Server Started:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lle41itll3c6ozfsic1y.PNG)
- Let's create a new Database and a new Collection

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wabr54fodjde550jc51b.PNG)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q76tb0w6wlqo84i8ti4x.PNG)

## All Dependencies: 

- Spring Data MongoDB

```
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
```

- Spring Web

```
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
```

- Lombok

```
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>
```

- Spring Boot DevTools

```
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>
```

- Spring Boot Validator

```
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
```
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
```

- For application.yml

```
spring:
  data:
     mongodb:
        authentication-database: admin
        port: 27017
        host: localhost
        database: EmployeeBackend
  application:
     name: employee-rest-api

  banner:
    charset: UTF-8
```
 
## Now you can see the project

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1z3mrrmwj9zs166a55u.PNG)

### Download All Packages

```
mvn package
```

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcu50bc6ti8mdrgeqsbr.PNG)

## Postman

### Testing With Postman

- Get all employees

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wakcdzilru2tie5hpmm.PNG)

- Save a new employee

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3xlivuovhgui5xmnj4v.PNG)

- Save another employee


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ws7pgg6o5voqtlwv7duj.PNG)

- Get all employees

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0r722o7zbb1pzlissmzt.PNG)

- Get employee by id


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x7x6hof3kj8wmkpxq821.PNG)

- Get employee by a valid id

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wli4189cwdo8p03qv9o.PNG)

- Edit employee by id

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekn78qsa65m29im3gazl.PNG)

- Delete employee by id

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mly3r3o62un6n2k45s1t.PNG)

- Get the number of employees that exist in the database

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8jzyh6dwb2ttpd20692.PNG)

You can see now all employees in the database

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gnelc1kldfm6etif5er.PNG)

#The source code examples used in this project can be found on Github Repository: {% embed https://github.com/ndourbamba18/spring-mongodb-rest-api-crud-example %}