Reactive Sprint Boot application using WebFlux
Tags: springboot, springwebflux, reactiveprogramming, spring
Published: 2018-09-05 13:04:43

---
title: Reactive Sprint Boot application using WebFlux
published: true
description: 
tags: #SpringBoot, #SpringWebFlux, #ReactiveProgramming, #Spring
---
A Reactive Spring Boot application can be written in
        **- Imperative style(Spring WebMVC)** or
        **- Declarative Style(Spring WebFlux).**

What is Reactive Spring Boot application?

it is a implementation of reactive streams(i.e sequence of data pushed from source to subscribers with back pressure), which are non-blocking, asynchronous and event-driven by nature. Spring Reactive libraries are build as part of Project Reactor projectreactor.io.

What is imperative style?

it is not imperative programming just a imperative programming style. it means Reactive Spring boot application can be written using sprint mvc annotations, so with minimum modifications, the old, legacy, big applications can also be converted to reactive nature and can use its features and benefits.

What is declarative style?

it is the latest functional lambda style of programming, this would be the better for new applications which are starting from scratch. let move further.

i recently converted my code base from Spring web-mvc to web-flux model. Initially, i faced few difficulties to understand and use Routers, Handlers and lambda functions, so i thought, writing a flat and simple controllers and equivalent Routers for same use cases will simplify the understanding.

As of now, spring boot apps can be developed in Spring WebMVC or WebFlux model, it is like... this or that, not a mix of both in a single application. so you shouldn’t have RestController annotation or spring-boot-starter-web dependency in pom.xml, if you are using spring web-flux routers.

REST endpoint should be defined on Configuration annotated class as a Bean of RouerFunction, Neither as a RestController nor Controller annotations.

Spring webflux is a programming paradigm used to compose Spring Boot applications on functional style(declarative style) using lambda functions.

![Alt text of image](https://thepracticaldev.s3.amazonaws.com/i/ylw0d0vqaz1tld0lakz3.png)

lets see some code on each model. i have used below bean and written few GET/POST/DELETE Rest endpoints.

Bean:-
{% gist https://gist.github.com/prabusubra/4d644360a90b690b934b24a4dc89fd01 %}
 
REST end points in Spring Web-MVC:-

Here everything is defined using annotations.
{% gist https://gist.github.com/prabusubra/99c19bc7799debecb5724c3c9f23562f %}

REST endpoints in Spring WebFlux:-

Few bunch of annotations are replaced with lambda functions, Not all the annotations. it is working with combination of annotations and lambda functions.

{% gist https://gist.github.com/prabusubra/24611497284965ff289195d07315a593 %}

For better re usability/structure of code, we can have a dedicated handler and utility classes as Spring Component or Service.

Conclusion:-

Eventually, Controller becomes Router. Request and Response related annotations become lambda functions, it may look like Spring framework is moving towards lambda function based configurations (XML → Annotations → lambda functions). but still annotations are playing huge role, even RouterFunction itself configured with Configuration and Bean annotations.

Refer the below git hub repository for code.

{% github http://github.com/prabusubra/microservicelearning %}

[Medium Post] (https://medium.com/letscode/sprint-boot-application-using-spring-webmvc-and-webflux-d94672b70595)

Thanks for spending your valuable time!!! please feel free to comment your views or suggestions…