DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Prabu Subra
Prabu Subra

Posted on

Reactive Sprint Boot application using WebFlux

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

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

Bean:-

REST end points in Spring Web-MVC:-

Here everything is defined using annotations.

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.

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.

microservicelearning

This repo is to learn about microservice architecture and related technologies.

REST endpoints for WebMVC:-

GET - http://localhost:7070/api/blogs
GET - http://localhost:7070/api/blogs/id/{id}
POST - http://localhost:7070/api/blogs
Request body :-
[ { "topicid": "webflux", "name": "webflux", "content": "Intro and operators", "author": "Josh Long", "category": null }, { "topicid": "react", "name": "React", "content": "Intro and operators", "author": "Josh Long", "category": null }, { "topicid": "projectreactor", "name": "projectreactor", "content": "Intro and operators", "author": "Josh Long", "category": null }, { "topicid": "restmvc", "name": "restmvc", "content": "Intro and annotations", "author": "Josh Long", "category": null } ]
DELETE - http://localhost:7070/api/blogs/id/{id}
REST endpoints for WebFlux:-
GET - http://localhost:6060/api/blogs
GET - http://localhost:6060/api/blogs/id/{id}
POST - http://localhost:6060/api/blogs
DELETE - http://localhost:7070/api/blogs/id/{id}




Medium Post

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

Top comments (0)

πŸ‘€ Just want to lurk?

You can still create an account and turn on features like 🌚 dark mode.