Spring MVC is one of my favorite web frameworks. Next major release of the popular web framework brings some interesting features. Along with Spring MVC, Spring Web Flux is coming later this year and it is coming here to stay. Spring Web Flux will help us to build high throughput functional reactive microservices.
What is reactive programming?
Reactive programming is programming with asynchronous streams . The magic two words here are asynchronous and stream. In reactive programming, everything is a stream. Unlike pull-centric Java 8 stream, reactive streams are push-centric. It uses the well known Observer Patern. Reactive programming is a wide topic and details are beyond the scope of this blog post. You can check the Reactive Streams, Reactive Manifesto, RxJava and this blog post.
- Java 8
In order to generate the project skeleton, go to http://start.spring.io address and choose “Generate a “maven” project with “Java” and Spring Boot “2.0.x”. As of writing this blog post, the latest version was 2.0.0 M2. In dependencies, section, choose Reactive Web and Reactive MongoDB. Then unzip the downloaded file and open with your favorite IDE. My personal preference is IntelliJ.
Currently, only MongoDB, Cassandra, and Redis are supported as reactive data stores. In this blog post, I will go with MongoDB.
In order to make use of MongoDB’s async API’s, we need to enable the reactive MongoDB repositories. The default configuration will connect you to the local MongoDB instance on the default port.
Our Todo model will consist of an id field, title, creation and completion dates and a boolean completion flag.
You can check out Project Lombok in order to create clean data models. It can magically create constructors, getters, and setters.
Creating this interface will magically generate common CRUD methods for us. The first generic argument is our domain model and the second is the type of the @Id annotated field. For MongoDB documents it can a String or a BigInteger.
The controller will be annotated with @RestController. Just like nonreactive rest applications.
Spring can inject dependencies in a constructor without @Autowired annotation but I prefer to declaring it explicitly.
Creating a Todo
In order to create a To-Do, we will use the following method.
Start the application by using the IDE or from the command line with
mvn spring-boot:run command.
Using the command line you can add some todos:
Listing all Todos
In order to list all todos, we will use the Flux class. Flux is just like Mono but there is a fundamental difference. A Mono can contain 0 or 1 elements in contrast to Flux can contain 0 to N elements. Flux is similar to a standard Java Stream or a List in that manner.
Filter ToDos by Title
We are going to add a custom query to our TodoRepository. It will query our MongoDB database and return results as a Reactive Stream.
Add the following line to TodoRepository.
Do not try to implement this method. Just leave it as it is. Spring data package will automatically implement the required method from the meaning.
And add the following handler method to the controller.
Now we can search Todos which contain the string in their titles.
Completing a ToDo
I have always liked the domain driven approach. So let’s add a complete method to our domain object.
We need to add a handler method to our controller.
As you see, we are following the functional programming fashion.
In order to test the endpoint, you can use the following command.
Just do not forget to replace the id section with one of your own todo ids.
Removing a Todo
It is very similar what we have done in the previous example. We will use
flatMap function to call the
delete method of the reactive repository.
Spring Boot 2.0 is coming with bunch of new features such as Functional Reactive Programming concepts. I am very excited to see what comes next. I hope this article helped to understand the basics of the reactive data repositories and reactive web applications.
You can check the full source code in this repo.