Skip to content

ndourbamba18/spring-mongodb-rest-api-crud-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot and MongoDB REST API Crud Example

E-Commerce Backend Application using Spring Boot & MySQL and Spring Data JPA & JWT

E-Commerce Rest Api Backend App

Crud Rest Api using Spring Boot & MongoDB

Spring Boot + MongoDB

Backend Authentication

Spring Boot + JWT Authentication + MySQL

Backend CRUD App

Spring Boot + MySQL Example Company Service App

Spring Boot + Spring MVC + MySQL Example Employee Manager App

Spring Boot + Java Mail Sender Example Contact Form

Spring Boot + MySQL Example Training App

Aws amazon lambda using Spring Boot

Front End

Angular Basic Example

Frontend App With Angular

Microservices:

Spring Boot + MySQL + Rest Template

Spring Boot + MySQL + Open Feign

Spring Boot + MySQL + Rest Template

More Practice:

Spring Boot + MySQL Example Microservice

Django + SQlite3 Example Login and Registration User

Spring Boot + MySQL Example Customer App

Building a REST API using Spring Boot & MongoDB

image-project

In this demo we are going to learn how to build a simple crud rest api backend application 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

Capture

Server started

Capture02

Let's create a new Database and a new Collection

Capture03

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 Spring WEB & Spring Data MongoDB.
spring-boot-mongodb

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>
  • dev tools
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>
  • Spring 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

Project

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

running

Postman

Testing With Postman

Get all employees

00

Save employee

01

Save employee again

02

Get all employees

05

Get employee by id

03

Get employee by a valid id

04

Edit employee by id

edit

Delete employee by id

delete

Get the number of employees that exist in the database

count

All data in the database

all-data

Other branches

main

docker-compose-demo

Swagger-demo

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages