Reactive Sprint boot application using Spring WebMVC and WebFlux

Prabu Subra
AlphaXcode
Published in
2 min readAug 15, 2018

A Spring Boot application can be written in

  • Imperative style(Spring WebMVC) or
  • Functional 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 style of programming. 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 suits for applications which are starting from scratch.

let move further…

Recently, I have converted a project from Spring webmvc to webflux style. Initially, i faced few difficulties to understand and use WebFlux Routers, Handlers and lambda functions, so i thought, writing a flat and simple webmvc controllers and its equivalent webflux Routers in a single page may simplify the understanding and broke the confusions around it.

  1. As of now, The Spring boot supports either Spring WebMVC or WebFlux style, 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.
  2. 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.

Spring MVC to WebFlux

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

Bean:-

Entity Definition

REST end points in Spring Web-MVC:-

Here everything is defined using annotations.

Spring WebMVC

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.

Spring WebFlux

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.

Thanks for spending your time!!! Feel free to comment your views or suggestions…

--

--