Reactive Programming Spring Boot, Spring Webflux with MongoDB Example, Spring Boot Reactive, Reactive REST API, Spring Webflux CRUD Example, How to develop a Reactive CRUD REST API with Spring WebFlux?, Reactive Stack vs Servlet Stack, Spring Reactive Client with WebClient etc.
Before directly going to the topic โHow to develop a Reactive CRUD REST API with Spring WebFlux?โ, letโs understand the fundamental concepts behind the Reactive Programming. Apart from REST Producer API, we will also learn how to develop a Consumer API here only. Letโs start discussing โHow to develop a Reactive CRUD REST API with Spring WebFlux?โ and itโs related concepts.
Why & When to use Reactive Programming?
In a Spring Boot MVC based web application, every request is internally a thread. This thread may communicate with the database until it gets data in response. This thread remains in blocking mode until it gets a response from the database. Also, this thread canโt be assigned to any other request while it is in blocking mode. Reactive Programming ensures application resource utilization optimally instead of keeping threads in idle mode. It makes resource utilization accurately by implementing an event loop.
When a thread makes database call, the call is handed over to event loop to process the SQL Query and thread becomes free for that duration. Now the thread in its free duration can process the other request parallelly. Subsequently, either same thread or another thread proceeds to serve the response. In our article โHow to develop a Reactive CRUD REST API with Spring WebFlux?โ we will discuss more than this.
To get the benefits of Spring Boot Reactive Programming, at server side, we can use Spring Web Flux. Also at DB side, we can use Spring Boot Reactive Database such as Mongo DB. As of now it has direct support of only NoSQL DB such as MongoDB and many more. Moreover, Spring Reactive is recommended instead of Spring REST. We will develop a Spring Webflux CRUD example to understand most of the concepts of Webflux. Letโs start discussing all about our topic โHow to develop a Reactive CRUD REST API with Spring WebFlux?โ in detail.
What Can You Expect from This Article?
In summary, you will be able to develop a REST producer API and the respective consumer API using Spring WebFlux in a single article itself. Once you complete going through all sections of this article, you will be able to answer :
1) What are the benefits of using Reactive Programming?
2) What does Reactive mean in Programming world?
3) What is Spring WebFlux?
4) Why Spring WebFlux?
5) What is the difference between Reactive Stack & Servlet Stack?
6) Minimum Software requited to support Reactive?
7) How to integrate with database in Reactive?
8) What is R2DBC?
9) Of course, โHow to develop a Reactive CRUD REST API with Spring WebFlux?โ
10) Hands-on of spring webflux crud example
11) Furthermore, How to develop/build a Reactive Client/Consumer Application using WebClient?
What is Reactive in programming world?
Letโs consider the term non-blocking. Non-blocking is reactive as instead of being blocked, it meant for reacting to notifications as soon as previous operations complete or data become available. In fact the term, โreactive,โ refers to programming models that are built around reacting to change such asโUI controllers reacting to mouse events or network components reacting to I/O events etc.
What is Spring WebFlux?ย
The primary web framework included in the Spring Framework, Spring Web MVC, internally uses the Servlet API and Servlet containers. Similarly to support the reactive-stack web framework, Spring WebFlux, was added later in Spring 5.0. It runs on servers such as Netty, Undertow, and Servlet 3.1+ containers and is fully non-blocking. Both frameworks have homogeneous names of their source modules: spring-webmvc and spring-webflux respectively. Applications can use either module. Sometimes both such as Spring MVC Controllers with the reactive WebClientย becomes the perfect combination.
Why was Spring WebFlux created?
There was a need of framework that can handle concurrency with a minimum number of threads, scale with minimum hardware resources and can also support non-blocking web stack. In other words, there was a need of reactive-stack web framework. Apart from that there was a need to promote functional programming. Needless to say, lambda expressions in Java 8 created opportunities for functional programming in Java. Also programmatically, Java 8 enabled Spring WebFlux to provide functional web endpoints with annotated controllers.
What is the difference between Reactive Stack and Servlet Stack?
Servlet Stack | Reactive Stack |
---|---|
Spring's web application module is spring-webmvc | Spring's web application module is spring-webflux |
Spring MVC based on the Servlet API | Spring WebFlux based on the ground to take advantage of multicore, next generation processors |
Uses a synchronous blocking I/O architecture | Is a non-blocking framework |
Works on the principle of one request per thread model. | Has capability to handle massive number of concurrent connections. |
By default uses Servlet Containers | By default uses Servlet 3.1+ Containers and Netty |
Uses Spring Data Repositories as JDBC, JPA, NoSQL | Uses Spring Data Reactive Repositories as Mongo, Cassandra, Redis, Couchbase, R2DBC |
Programming model uses Servlet API | Programming model uses Reactive Streams Adapters |
Spring's Security module is Spring Security Reactive | Spring's Security module is Spring Security |
What is minimum Software required to support Reactive?
โฆ Spring 5.x to support Spring Web Flux
โฅ Servlets 3.1+
โฆ Spring Boot uses Netty Server by default as it is well-established in the asynchronous, non-blocking space.
How database integration happens in Reactive way?
Accessing and processing data via database in a reactive way is also equally important. NoSQL databases such as MongoDB, Redis, and Cassandra all have native reactive support as part of Spring Data. Moreover, many relational databases such as Microsoft SQL Server, MySQL, H2, Postgres and Google Spanner have reactive support via R2DBC(Reactive Relational Database Connectivity).
What is R2DBC?
R2DBC stands for Reactive Relational Database Connectivity, offers integration of relational databases in a reactive application stack. As of now only NoSQL databases have native reactive support in Spring Data. R2DBC acts as an incubator to integrate relational databases using a reactive driver. Spring Data R2DBC applies popular Spring abstractions and repository support for R2DBC. It offers an easier way to build Spring-based applications that use relational databases such as Microsoft SQL Server, MySQL, Postgres, H2 etc. in a reactive application stack.
How to develop a Reactive CRUD REST API with Spring WebFlux?
To illustrate the actual reactive programming, we will develop a spring webflux crud example. Needless to say, CRUD is nothing but an abbreviation for Create, Retrieve, Update & Delete operations. Here we will consider โInvoiceโ as an entity to develop the API. Letโs start our exercise by following step by step instructions.
Software/Tools used in this Project ?
โฆ STS (Spring Tool Suite) : Version-> 4.7.1.RELEASE
โ Dependent Starters : โSpring Reactive Webโ, โSpring Data Reactive Mongo DBโ, โLombokโ and โSpring Boot DevToolsโ
โฅ MongoDB 3.6.20 2008R2Plus SSL (64 bit)
โฆ JDK8 or later versions (Extremely tested on JDK8, JDK11 and JDK14)
Step#1: Create Project using STS(Spring Tool Suite)
If you are new to Spring Boot, visitย Internal Link to create a sample project in spring boot. While creating starter project in STS, select starter dependencies asย โSpring Reactive Webโ, โSpring Data Reactive Mongo DBโ, and โLombokโ. You can also add โSpring Boot DevToolsโ optionally.
If you are new to โLombokโ, kindly visit โHow to configure Lombokโ and to know all about it in detail. Please note that we are using Mongo DB as database because reactive stack has direct support of only NoSQL databases. If you still have not installed Mongo DB in your system, follow the instructions to install it from here. To get an easy grasp on โHow to work with Mongo DBโ, please visit Mogo DB tutorial.
Step#2 : Update database properties in application.properties file
Update application.properties to connect with Mongo DB accordingly as below.
# application.properties ---------------------------------------- spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=reactivedb
Step#3 : Create Invoice Entity & Repository interface
Now create Invoice.java and InvoiceRepositoty.java as below. InvoiceRepository.java will be an interface which will extend ReactiveMongoRepository<Invoice, Integer>.
// Invoice.java ---------------------------------------------------------------- package com.dev.springboot.reactive.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import lombok.Data; @Data @Document public class Invoice { @Id private Integer id; private String name; private String number; private Double amount; }
// InvoiceRepository.java ------------------------------------------------------------------------------------ package com.dev.springboot.reactive.repo; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import com.dev.springboot.reactive.model.Invoice; public interface InvoiceRepository extends ReactiveMongoRepository<Invoice, Integer> { }
Step#4 : Create Service Interface & Service Implementation class
Create Service Interface and Service Impl class as IInvoiceService.java and InvoiceServiceImpl.java accordingly as shown below. Please observe the usage of Mono & Flux in service methods. Use Mono for single object and Flux for multiple objects as return type.
Step#5 : Create InvoiceController classย
Subsequently, write a RestController for Invoice as โInvoiceController.javaโ as below.
How to test created Reactive REST API?
Before testing the created REST API, start your Mongo DB instance. Now you can use the most popular POSTMAN tool to test your API. However, we are going to develop a Reactive Consumer application using WebClient in the next section. If you can wait until then, you will also get knowledge of Reactive Consumer App. Even if you are new to POSTMAN tool, kindly visit our internal article on โHow to test REST API with POSTMAN.
How to develop a Reactive Client Application with WebClient?
As we already know, for non-reactive REST producer(Servlet Stack REST producer), we generally use RestTemplate in order to develop a consumer. Please also note that Client Application or Consumer Application both we use in the same sense. Here in place of RestTemplate we will use WebClient that supports making request to Reactive RestController and gets final Data either as Mono or as Flux. Moreover WebClient is an interface (DefaultWebClient is an impl class)
that is used to define Reactive Client Application.
Guidelines to develop Reactive Client Application with WebClient
Please follow below guidelines to create a Client application using WebClient.
- Create WebClient Object using Base URL.
- Provide Path at Controller method using Request METHOD(GET/POST)
- Provide Inputs if exist (Body, Params)
- Create Request for data retrieval with Type mono/flux
โฅ Note : Model class must exist exactly same as in Producer Application.
Step#1: Create Project using STS(Spring Tool Suite)
Create one Spring Boot Starter Project as SpringBoot2ReactiveClientApp. While creating starter project in STS, select starter dependencies as โSpring Reactive Webโ and โLombokโ. You can also add โSpring Boot DevToolsโ optionally. If you are new to โLombokโ, kindly visit โHow to configure Lombokโ and to know all about it in detail.
Step#2 : Update server properties in application.properties file
You need to update the server port and it should be different from the producer applicationโs server port as you might be running both applications from the same system. Update it something like below.
server.port=9090
Step#3: Create Model class Invoice.java
Please make sure this model class must be the same as it is in the producer application particularly in terms of fields and their datatypes.
Now its time to really test the API and get the results accordingly. Here we are using Runner classes to test each method in a separate Runner class as in step#4 to step#7. The best way to test and get correct results is that keep @Component at one Runner class at a time and comment it on others which are not part of your current testing.
Step#4: Runner class to fetch/retrieve all Invoices
Step#5: Runner class to fetch/retrieve one Invoice
Step#6: Runner class to save or update Invoice
Step#7: Runner class to delete Invoice
Conclusion
After going through all the theoretical and spring webflux crud example part of โHow to develop a Reactive CRUD REST API with Spring WebFlux?โ, finally, we should be able to develop a Reactive CRUD REST API as a producer application with Spring WebFlux in a Spring Boot project. Also, we should be able to develop a Consumer/Client Application using WebClient. Similarly, we expect from you to further extend these examples. Also try to implement them in your project accordingly. In addition, If there is any update in the future, we will also update the article accordingly. Moreover, Feel free to provide your comments in the comments section below.
Nice article.
But, It would be better if You put the code on VCS like github or gitlab. So that We can compare the code.
Thanks
Yes we will put the code in a repository & share the link. Thanks for your feedback.