Spring Webflux — Part 2

Ayesh Almeida
2 min readMay 11, 2021

Reactive Programming is one of the most popular programming paradigms now a days. Spring Webflux is the newest flavor of Spring Framework with the taste of Reactive Programming. In my first article I have discussed about the building boxes of . So, in this article we will build a simple application with Spring Webflux.

Application Structure

We will create a Spring-boot Application as a Maven Project. Add the following dependencies to Maven-POM.

Annotate the Spring-Boot-Application class with @EnableWebFlux. Adding this annotation to an ‘@Configuration class’ or ‘Spring-Boot-Application class’ imports the Spring Web Reactive configuration from WebFluxConfigurationSupport.

Building the Application

Spring-Boot is so simple as you can use Spring-Webflux as you use Spring-MVC. You can just use Spring-Controllers. Following is a sample how to use controllers with Spring-Webflux.

If you run the application and access ‘http://localhost:8080/index' in your browser you will get a response of ‘Hello World!’. And it is what you have to do to say ‘Hello World!’ in Reactive. But this is the most traditional way you do in Spring. But Reactive Programming is always comes with Functional Programming.

You can use Webflux in more functional way. You can write your own router functions and handlers for those routes. Routes are more likely the resource-paths. When you do a request to the resource path with the correct HTTP method the handler will handle the request and respond.

First lets write the handler function. A handler function will take a ServerRequest and returns Mono<ServerResponse>.

Now we can write the router for the handler. Whenever a request comes to the resource-path of the router it will calls the handler function and respond to the request.

Now if you run the application and access ‘http://localhost:8080/simple-route' from your browser, you will get ‘Sample Route Successful!’ as the response. So, that is how we can be more functional in Spring Webflux. In the next article we will discuss about more advance routing functions. The complete source code for this article can be found here.

Originally published at http://ayeshforyou.blogspot.com.

--

--

Ayesh Almeida

A Tech Enthusiast who wants to build simple solutions for complex problems.