Spring Web Flux Initial Concept
A brief discussion on the Initial Concept of Spring Reactive Programming is described in this springcavaj – Spring Web Flux page consists of the different reactive methods used in Reactive Programming.
This guide will provide a better understanding on the different reactive methods used in Reactive Programming. Use of Mono and Flux in Reactive Programming.
Advantages of using Spring Boot with Web Flux
Spring Boot Framework provides an immense support to use Web Flux for Reactive Programming in support with Maven. A dependency named as spring-boot-starter-webflux can be used to explore the libraries, APIs, used for Reactive Programming.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
In this blog, I will provide you a lot of details regarding Web Flux Reactive Programming. Like,
- To save a record in DB
- To get all records from DB
- To get specific record from DB by either Primary key column or by using another column
- Get Parent Child records
- Get historical records
- Generate PDF from that historical records
- Update a record
- Delete a record
In case of communicating to a Database from a Reactive Programming, we don’t use the traditional approach. In case of Reactive Programming, we use R2DBC connection and it is also provided by Spring Boot Framework.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
The above spring-boot-starter-data-r2dbc dependency is used to connect any RDBMS DB. Here in this blog, I have used MySQL as the RDBMS DB. Even for that MySQL DB connection, we need to use another R2DBC Dependency named as r2dbc-mysql
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>0.8.2.RELEASE</version>
</dependency>
For PDF generation, I have used the dependency named as itext7-core
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>8.0.5</version>
<type>pom</type>
</dependency>
Using Spring Web Flux with Spring Boot and using R2DBC Connection with MySQL DB
I have created a demo project named as spring-reactive-web-flux-masterclass and uploaded it to my personal GitHub account. One can clone the project and run it locally in their system. But before that, the prerequisite software needs to be installed on the local machine. All the software lists are provided in the README.md file of the above repository. And after that in the same README.md file I have provided the steps to install, clone, and run the application locally.
In the same README.md file, I have also provided an extra part that how you will create the schema, tables in the MySQL DB.
- Brief Description – I have developed an application using Java17, Spring Boot, Maven, Spring-Data-R2DBC, Spring Web Flux, MySQL DB to achieve how to do Reactive Programming using Spring Web Flux. The main goal of this application is to understand the user how to do reactive programming, which is slightly different from our traditional or synchronous programming. By the way, Reactive Programming is also known as Asynchronous Programming.
- Software Used – Software required to develop this application.
- IntelliJ IDEA Educational Edition 2022.2.2 – If the latest version is available, then download that one
- Apache Maven 3.6.3 – If the latest version is available then download that one
- Java 17 – Not less than Java 8, you can also use Java 8
- Git 2.27.0 – The latest version as available
- MySQL Workbench 8.0 CE – MySQL DB Server – Community Edition
- SQLYog/DBeaver – One can use SQLYog/DBeaver in replacement of MySQL Workbench
- Postman v8.3.0 – To test the REST Service
- Project Components – The above application uses Spring Boot, Maven, MySQL DB to support reactive programming. For that I have used one of the dependency named as spring-boot-starter-webflux, and to connect to MySQL DB 2 dependencies I have used they are spring-boot-starter-data-r2dbc and r2dbc-mysql.
To connect to the MySQL DB using R2DBC Connection, I have defined 3 properties in application.properties file as follows:
spring.r2dbc.url=r2dbc:mysql://localhost:3306/flux
spring.r2dbc.username=root
spring.r2dbc.password=root
- Structure of the Project – Using Spring Data with R2DBC and Spring Web Flux, your project should follow a specific structure. Please find below the screenshot of that structure.
- Testing the application – There is no UI component for this application, so I have used Postman to test the REST API Endpoints. But you can integrate the UI segment and can do proper POC on your own.
- Clone the application from GitHub link import in IntelliJ IDEA ID
- Once you import the application, run the application by setting an option available in the top right corner of the IntelliJ IDEA ID. Screenshot is provided.
- Once you have that application set, now you can Run/Debug your application.
- Once you click the Run option to run your application, the application will start and run on port no 7124.
GitHub Code Link
Download the Source Code from GitHub
Common Faced Problems
Spring Web Flux Reactive Programming Common Problems
Interview FAQs
Spring Web Flux Reactive Programming Interview FAQs
Other Useful Links
Spring Data using RDBMS (MySQL DB) and Spring REST
Spring Data using NoSQL DB and Spring REST
Spring Data using Cypher Neo4j DB and Spring REST
Spring Data using Redis and Spring REST
Spring Apache Kafka – Producer & Consumer
Spring Kafka Confluent – Set Up
Spring Kafka Confluent – Producer & Consumer
Spring Cloud using Google Cloud SQL & Google Cloud Storage