Crud avec Spring Boot et MongoDB
Tags: java, springboot, springjpa, restapi
Published: 2020-04-16 13:24:28

---
title: Crud avec Spring Boot et MongoDB
published: true
description: 
tags:   java, spring_boot, spring jpa, rest api
---
Ziar naleen sama guy yi (Just to say hi)

Today we will learn how to create a **REST API** with **Mongo DB** using **Spring Boot** and perform basic operations with **Spring JPA** and **Spring MVC**.

## Spring Boot ???
**Spring Boot** is a framework that facilitates the development of applications based on Spring by providing tools for building a packaged, fully standalone application.

## MongoDB ???
**MongoDB** is a database management system or DBMS, like Mysql or PostgreSql, but with a completely different approach. Gone are the days when you had to create a relational table schema and create complex Sql queries. Thanks to MongoDB you will be able to store your data a little bit like you would in a JSON file. That is to say, a kind of giant dictionary composed of keys and values. These data can then be exploited by javascript, directly integrated in MongoDB, but can also be exploited by other languages like python and of course Java :) .
As a database of type **NoSQL**, MongoDB stores the data in the form of a document. Thus, it offers more flexibility.

We don't like to talk too much. We will follow this tutorial together to make a crud. I'll try to explain each part in detail *Athiaa bokk...* !!!! 
# 1- Tools

We have noted some needs to carry out the project.

    java, JDK 8
    maven
    MongoDB
    Postman
( I used intelliJ as IDE)
    

# 2- Create the project

We will use [https://start.spring.io/](https://start.spring.io/) to generate our project. We added as web and mongoDb dependencies.
![Alt text of image](https://miro.medium.com/max/2000/1*R0gxMGBihjWUB6oarIBOVA.png)

# 3- Project structure

Under intelliJ here is our project structure. This may change depending on the IDE. (Eclipse 🤮)

![Alt structure](https://miro.medium.com/max/750/1*1ARHKmhuOPlI9lVT0F8PGQ.png)
# 4- Our Maven Dependencies
![Alt dependencies](https://miro.medium.com/max/1392/1*WQ-YpQuv8OCmsDSEfAUQ7w.png)
Here is after generation the content of our pom.xml file.

# 5- Our Application Properties file
For the database configurations, we used the application.properties file found in resources.

![Alt application.properties](https://miro.medium.com/max/984/1*2-k-8uifKUPeBY6LJl4k0w.png)
# 6- Java Classes

###### - Main class : **CrudSpringBootMongoDbApplication.java**
Always remember that the entry point of the Spring Boot application is the class containing the **@SpringBootApplication** annotation and the main method in static.
![Alt dependencies](https://miro.medium.com/max/1400/1*m0CAReghhf1ke0zKuC2lsg.png)

###### - Model class:
annotation *@document* marks a class as being a domain object that we want to persist to the database.
![Alt User.java](https://miro.medium.com/max/1400/1*JDBLYceiRkl1sYlFZxDueQ.png)

###### - Data-Access-Object interface
UserDao.java interface that extends MongoRepository to automatically manage our raw requests.

![Alt ](https://miro.medium.com/max/1120/1*CLP0NElN25fiysqkMRUmig.png)

###### - Service class UserServiceImpl.java
Service class where we will call the methods of the Dao interface to manage SQL operations. It implements our **UserService.java** interface.
![ Alt ](https://miro.medium.com/max/926/1*9wTcA7L9nN1xTUL8u0Zwxg.png)
![ Alt ](https://miro.medium.com/max/1378/1*gkyrsZKvsSc_bgm3-RY0Ow.png)
###### - Controller class UserController.java 
Allows to process requests. The class is annotated with @RestController where each method returns a domain object as a json response instead of a view.
![Alt ](https://miro.medium.com/max/1400/1*I_CVqsaCgldJ3DA9IeaEcg.png)

Demostration ( *thiaw sa khirr*)

Execute the following command:

        mvn spring-boot:run
![Alt mvn](https://miro.medium.com/max/2000/1*C1jAx8z0spqR3u2t32gdEA.png)

or go into folder target and execute:  

        java -jar crudSpringBootMongoDb-0.0.1-SNAPSHOT.jar

![Alt java](https://miro.medium.com/max/2000/1*nBeDLjNDTTlKz3h_hChoSA.png)

// Create new user.
http://localhost:8088/api/mongo/users/create
// Get all users.
http://localhost:8088/api/mongo/users
// Find user by id.
http://localhost:8088/api/mongo/users/1
// Update user by id.
http://localhost:8088/api/mongo/users/5
// Delete user by id.
http://localhost:8088/api/mongo/users/1
// Delete all uers.
http://localhost:8088/api/mongo/users/deleteall

### [Source code](https://github.com/tonux/crud-springboot-mongoDB)