Building a REST Service with Spring Boot and MongoDB (Part 1)

Yiğitcan Nalcı
Apr 27, 2019 · 6 min read

I’ve decided to learn Spring Boot after using the previous versions of Spring for years. I realized that Spring Boot makes a lot of things pretty easy. Besides, it reduces complexity of your dependencies. All you need to do is run your application.

In this tutorial, I will explain “how to develop a REST service with Spring Boot and MongoDB”.

  • Spring Boot and Spring Data projects which is used for developing REST APIs are used here.
  • Maven is used to automate the build process.
  • A typical domain-driven development approach is used, separating model, repository, service, and controller classes.

You can download the project source code from this GitHub link.

Technologies and Tools

The tutorial here uses IntelliJ IDEA, Maven, and Postman for coding, building, and REST API testing. I’m running on Windows 10 using PowerShell.

All software is written in Java, with Spring 5.1 and Spring Boot 2.1.4. MongoDB 4.0.9 Community Server is used for persistence.

From here, I’ll assume you have JDK 8.0, IntelliJ, Maven, MongoDB, and Postman installed.

Student Information Service

This REST service brings student information and allows you to add new students to the system or remove students from the system.

My goal here is to show how the REST services will be developed using this technologies in a simple way without shuffling the business logic.

  • A POST operation is used to add students to the system.
  • GET operations are used to get students by student number or email. Students are also brought according to their GPAs.
  • The students collection in the MongoDB database has the data about each student’s name, student number, email, course list and GPA.
  • Everything stored, received, and returned by the service is formatted as JSON.

Step 1: Spring Initialzr

If you want to create a new Spring application, you can use the Spring Initalizr. I entered the values for this project as in the picture below.

Spring Initializr setup
  • Select Maven Project, Java, and Spring Boot 2.1.4.
  • Pick an appropriate group and artifact.
  • The two dependencies: MongoDB and Web.

After doing that, you can download a template project as .zip file with “Generate Project” button.

Unzip the project and import into IntelliJ. After doing that, you will see something like the picture below.

Package structure in Intellij

Step 2: Model and Repository

Student Model

A Student has six fields: id, name, studentNumber, email, courseList and gpa.

@Document annotation is used for the same purpose with @Entity annotation in JPA.

For more information about Spring Data MongoDB, you can use this link. You can read the document for the version you want. For this tutorial, the version of Spring Data MongoDB is 2.1.6.

Student Repository

With the Spring Data project, you will have magic when you create an interface that extends MongoRepository. The magic is shown below. There is no need to implement this interface :).

Student repository interface has three methods. Firs two methods find the student by its student number or email. The last method sorts students according to their GPA.

Step 3: Student Service

Now, we create the student service that the student controller will call. The student service has six methods. Simply, these methods list students according to some criteria, save a student, update a student and delete a student.

Normally, controller classes talk with the methods in repository classes directly, there is nothing wrong. But, if we need to some business logic, we should not write this code block in controller. Because of this, the service layer is needed.

Step 4: REST Controller

Finally, we can create the REST controller. This controller has four @GetMapping, one @PostMapping and one @DeleteMapping.

Step 5: Database Configuration

To connect to the mongoDB database, edit the settings in the “” file which is part of “resources”. The following configurations are sufficient because there is no authentication in the database currently.

Running the Service and Testing with Postman

To run the service and test it in Postman, first, we need to run MongoDB. For this, two commands are required.

mongod command
mongo command

After doing that, we can run the project to test the REST service.

Run the project

After running the project, you can test the REST service. For this, open Postman, enter the URL and select GET method like below.

Empty student list

You can add a student to the system with POST method in Postman. First, you should enter Content-Type : application/json header key-value pair. Second, in body tab, you must enter the student information that you want to save the database in JSON format.

Adding a student
List of students by GPA

If you want to delete a student, you should select DELETE method in Postman and add a student number that you want to delete to the end of the link.

Deleting a student

After doing the steps above, you should see two records in the database like below.

The students collection

Note: This tutorial focuses on the combination of Spring Data, MongoDB and REST API. There is no validation for object fields in the project.

Please click on the link below to learn how to write a unit test using Spring Boot.

Writing a Unit Test using Spring Boot (Part 2).

Thank you for reading! 🙏 Your thoughts are very valuable to me. Please feel free to share. 😄

Yiğitcan Nalcı

Written by

Software Engineer. Sports lover. Curious about science and technology. Novice writer for now.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade