Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

MongoDB is one of the database systems that supports the Reactive mechanism through the use of its MongoDB Reactive Streams Java Driver. Together with Spring Data MongoDB, Spring also introduces us to the Spring Data MongoDB Reactive module which helps us minimize repetitive code when using the MongoDB Reactive Streams Java Driver. In this tutorial, through an example of building Reactive REST APIs, I show you how to use Spring Data MongoDB Reactive with Spring WebFlux.

First, I will create a Spring Boot project with Reactive MongoDB and Reactive Web as follows:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

as an example.

Result:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

Project Lombok:

In this example, I will create an application that allows users to add new student information and get a list of students with data stored in the MongoDB database. When a user requests to our application, whenever a new student is added, the student’s information is published to the user.

Before going into the main content of the tutorial, there are 2 things I need to tell will you all first.

First, I will tell you some information about Spring Data MongoDB Reactive.

Similar to Spring Data MongoDB, an interface named ReactiveMongoRepository was introduced in Spring Data MongoDB Reactive. We have the Repository interface as the main interface in the Spring Data project. And with the Reactive model, we have the ReactiveCrudRepository interface, ReactiveSortingRepository extends from the Repository interface to serve this model. And the Spring Data MongoDB Reactive’s ReactiveMongoRepository interface extends from the ReactiveSortingRepository interface. You will see this through the following image:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

As you can see, the implementation of the ReactiveMongoRepository interface is the SimpleReactiveMongoRepository class.

The second is for our application to push student information whenever it is added, we need to use MongoDB with the capped collection.

The reason is that by default, for each query, when the user has exhausted the data for that query, MongoDB closes the cursor. Cursor in MongoDB you can understand that it is a pointer in a result set of a query. MongoDB’s close cursor makes it impossible to continue receiving data from our query.

To solve this problem, we will use the tailable cursor with MongoDB’s capped collection. Capped collections are fixed-size collections, meaning we can only add a certain number of documents. When the capped collection is full, the new document will override the oldest document. For the tailable cursor, if you used the tail -f command in Linux, you can imagine that tailable cusor will keep retrieving document after the user has retrieved the result for the query.

You can create a capped collection using the following command in the MongoDB Shell:

In the example of this tutorial, I will create a capped collection named student as follows:

Result:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

OK, now we will go into the main example of this tutorial.

First, I will create an interface named StudentRepository extends from Reactive MongoRepository with the following content:

The Student class has the following contents:

As you can see, although ReactiveMongoRepository has provided us with the findAll() method to retrieve all student lists, but this method does not support the tailable cursor, we must define a findBy() method. It has the same functionality as the findAll() method with the @Tailable annotation. The findBy() method will allow us to use the tailable cursor function to keep retrieving document after the user has retrieved the result of the query.

Next, I will configure the connection to the MongoDB server.

Since our example project is a Spring Boot project, it is much simpler to configure the connection to the MongoDB server. We will not need to instantiate the MongoTemplate object. To do this, open the application.properties file and depending on whether you are using MongoDB server with authentication mode or not, configure as below.

For authenticate mode, we need to declare the username and password as follows:

Otherwise just declare:

is to be.

Currently, I use MongoDB server without username, password so I just declare as follows:

The last thing we need to do is add Controller to expose 2 requests: a request that allows the user to retrieve the student list and a request that allows the user to add new student.

I will create StudentController with the following content:

As you can see, I have built 2 requests: “/students” with the GET method and produces “text/event-stream” which allows users to retrieve all student lists and whenever new students are added, this student’s information will be pushed down to the user; “/student” with the POST method allows us to add new student.

The ReactiveMongoRepository has provided us with the findAll() and save() methods, so we just call them for use. One point to note is that, instead of returning to a List<Student> as in Spring Data MongoDB does, here Spring Data MongoDB Reactive will return a Flux<Student> object and similarly the save() method. Also, it returns a Mono<Student> object.

OK, here we have completed the necessary steps to build this example application. Try it now.

I will open a browser window to run the “/students” request and use Postman to run the request “/student” to add a new student.

I will use Postman to add new student first:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

At this time, the request “/students” will have the following output:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

Now, if you add another student:

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

As you will see, the student’s information is automatically pushed to the browser window with the “/students” request.

Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

One thought on “Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux

Add Comment