Reactive Programming (Part 2)

Yiğitcan Nalcı
Javarevisited
Published in
4 min readAug 13, 2022

--

Hi again, I hope all is well! I feel happy with the photo below, I suggest that to feel the same with me, take a deep breath and look at the photo 😄.

Photo by Aaron Burden on Unsplash

As I said before, I am here with a practical coding example of Reactive Programming.

Figure from https://spring.io

In this tutorial, I will explain “how to develop a reactive mail service with Spring WebFlux and MongoDB.

Technologies and Tools

The tutorial here uses IntelliJ IDEA, Maven, and Postman for coding, building, and REST API testing. I’m running on MacOS Monterey.

All software is written in Java, with Spring Boot 2.6.2. MongoDB 4.2 Community Server is used for persistence.

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

Reactive Mail Service

This microservice provides two endpoints. First, for sending simple emails and for sending emails with attachments. Also, this service saves the email’s data to the MongoDB database.

Photo by Solen Feyissa on Unsplash

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

  • A POST operation is used to send an email.
  • Another POST operation is used to send emails with attachments.
  • Everything stored, received, and returned by the service is formatted as JSON.

MailDocument Model

This model has 6 fields: id, from, to, cc, bcc, time and MailDocument is used to save data to the database.

MailDocument.java

Mail Repository

You can easily create an interface extending ReactiveMongoRepository using Spring Data Project. With this, there is no need to implement this interface.

MailRepository.java

It is important to use ReactiveMongoRepository instead of MongoRepository because, with this interface, we provide reactiveness from presentation to persistence.

Mail Service

The mail service has two public and five private methods. Simply, these methods provide sending emails with or without attachments. After sending emails, email data is saved to the MongoDB database.

MailService.java

Mail Controller

MailController.java is a REST controller which has two @PostMapping.

MailController.java

Database Configuration

You need to start MongoDB once to connect to the database. You can write the below command if you use brew.

brew services start mongodb-community@4.2

If you see this message on the terminal window, you are on the right way.

To connect to the MongoDB database, edit the settings in the “application.properties” file which is part of “resources”. The following configurations are sufficient.

#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=test

Running the Service and Testing with Postman

You can easily run this project to test the REST endpoints.

Run the project in IntelliJ

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

Send an email

You can send an email to the specified email adress POST method in Postman. First, you should enter Content-Type : application/json header key-value pair. Second, in the body tab, you must enter the email information that you want to save the database in JSON format.

Email sent

By the way, you can also test the other POST method by providing accurate JSON objects.

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

Resources

--

--

Yiğitcan Nalcı
Javarevisited

Dad. Software Engineer. Sports lover, doer. Curious about science and technology. Novice writer for now. https://www.linkedin.com/in/yigitcannalci