Spring Boot — Reactive Web
Tags: vmware, java, spring, springframe
Published: 2022-06-16 17:08:00

---
title: Spring Boot — Reactive Web
published: true
date: 2022-05-27 17:55:47 UTC
tags: vmware,java,spring,springframe
canonical_url: 
---

### Spring Boot — Reactive Web

### Introduction

In [this](https://apurvsheth.medium.com/h2-in-memory-database-with-spring-boot-1295f3302c93) article, we learned how to use Spring Data **JDBC** connectivity.

Today, we will learn to build an application that uses Spring Data **R2DBC** to store and retrieve data in a relational database using **reactive database ** drivers.

### Tools/Frameworks

**IDE**  — IntelliJ Idea/Eclipse  
**Language**  — Java 8 or above  
**Framework**  — Spring boot, Project Reactor  
**Database**  — H2  
**Build Tool**  — Maven

### Key Concept(s)

The heart of the project reactor is **reactive-core**. The main focus is on the Reactive Streams spec and targets Java 8 streams.

Project reactor introduced reactive types Flux & Mono that implement Publisher.

**Flux**  — It represents an asynchronous sequence of zero or more objects

**Mono** —It represents asynchronous single or empty objects

The image given below very well describes the flux transformation very well. Please refer to the reference link at the end of the article for further readings.

![](https://cdn-images-1.medium.com/max/932/1*_QeKx2MT20CPmf0bkBuRdw.png)
_Flux Transformation from projectreactor.io_

Since we have gone through the basic concepts, let's jump on the implementation without having any delay.

### Maven Dependency

![](https://cdn-images-1.medium.com/max/644/1*gFAh3dmGs3FB8UmvdRICjQ.png)
_Reactor — Maven Dependency_

### Configuration

First. let’s go through the configuration. As you have noticed, we have used the r2dbc configuration & the URL is prefixed with r2dbc but not with JDBC.

![](https://cdn-images-1.medium.com/max/392/1*tRmE43Ir3_3yuRqhjN9tgg.png)
_R2DBC Configuration_

### Implementation

Now, let’s have a look at the implementation. To have a reactive nature, we will implement ReactiveCrudRepository as shown below.

![](https://cdn-images-1.medium.com/max/906/1*oZ-BuEbTvvCtBTKIG0HRKw.png)

In our service class, we will return the Flux/Mono depending on our functionality.

![](https://cdn-images-1.medium.com/max/689/1*qDwZbsJ7NbMndEQIMLJmFA.png)
_Example — Flux/Mono Service_

In our API resources also, we will return flux/mono as a response as shown below. However, the point to note down here is, that we need to set the “produces” media type as “MediaType._TEXT\_EVENT\_STREAM\_VALUE_”.

![](https://cdn-images-1.medium.com/max/857/1*JOvItRBaO86UO4gsAVXSCg.png)
_API/RestController with TEXT_EVENT_STREAM_VALUE media type_

Huh, you might think, enough concepts & implementation but does it really work! Of course, it does, :) Let's go ahead & test.

### Testing

I’m not sure if you have observed earlier, that I have kept a delay of 5 seconds before emitting the next objects so that we can verify that it's non-blocking asynchronous emitting of objects.

![](https://cdn-images-1.medium.com/max/1024/1*EIokGH0dfAwB2ueUXE_ztg.gif)
_Testing_

Yey, we have successfully implemented spring boot reactive web flux & verified the same.

**If this post was helpful, please clap for few times or follow to show your support.**

### Git Repo

[spring-boot/spring-reactive-flux-example at main · shethapurv/spring-boot](https://github.com/shethapurv/spring-boot/tree/main/spring-reactive-flux-example)

### References

[Project Reactor and the Spring portfolio work together to enable developers to build enterprise-grade reactive systems that are responsive, resilient, elastic, and message-driven.](https://spring.io/reactive)

[https://projectreactor.io/](https://projectreactor.io/)

[Reactor 3 Reference Guide](https://projectreactor.io/docs/core/release/reference/#intro-reactive)