DEV Community

NDONGO TONUX SAMB
NDONGO TONUX SAMB

Posted on

Crud avec Spring Boot et MongoDB

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/ to generate our project. We added as web and mongoDb dependencies.
Alt text of image

3- Project structure

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

Alt structure

4- Our Maven Dependencies

Alt dependencies
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

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

- Model class:

annotation @document marks a class as being a domain object that we want to persist to the database.
Alt User.java

- Data-Access-Object interface

UserDao.java interface that extends MongoRepository to automatically manage our raw requests.

Alt

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

- 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

Demostration ( thiaw sa khirr)

Execute the following command:

    mvn spring-boot:run

Alt mvn

or go into folder target and execute:

    java -jar crudSpringBootMongoDb-0.0.1-SNAPSHOT.jar

Alt java

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

Top comments (0)