How to build SpringBoot MongoDb RestfulApi

In the tutorial, JavaSampleApproach will show you way to build a SpringBoot MongoDB RestfulApi.

Related Articles:
Spring MongoOperations to access MongoDB
How to use SpringData MongoRepository to interact with MongDB
How to access MySQL database with Spring Data Rest application
How to use HAL Browser with Spring Data Rest | SpringBoot + MySQL


I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Boot: 1.4.3.RELEASE
– Spring Tool Suite – Version 3.8.1.RELEASE
– MongoDB v3.4.1

II. Overview
1. Project Structure

springboot mongodb restfulapi project structure

2. Step to do

– Create SpringBoot project
– Create model class
– Create MongoDB Repository
– Run & Check Result

III. Practice
1. Create SpringBoot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input needed project’s info. Press Next, then Finish, a SpringBoot project will be created succesfully.
Open pom.xml file, add Spring Data Rest & Spring Data Mongo dependencies:

2. Create model class

Create a Customer class for simple model:

3. Create a MongoDb repository

Create a MongoDb Repository by extends interface MongoRepository:

In above code, we define a customize method for finding a list customers by lastname.

Open application.properties to configure MongoDB:

4. Run & Check Result

Run MongoDB Server. Then run Spring Boot project.

GET request:
localhost:8080
springboot mongodb restfulapi get request

localhost:8080/customer
springboot mongodb restfulapi get customer request

POST request:
springboot mongodb restfulapi post request

GET all Customers:
springboot mongodb restfulapi get all request

– All search query:
springboot mongodb restfulapi get all search request

– Find customers by LastName:
springboot mongodb restfulapi find by last name request

MongoClient

IV. Source code

SpringBootMongoDbRestApi

By JavaSampleApproach | January 11, 2017.

Last updated on July 7, 2017.


Related Posts


2 thoughts on “How to build SpringBoot MongoDb RestfulApi”

  1. I had 404 error in the rest client . I use Insomnia, both get and Post method gave 404 INVALID.
    Could you please help.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*