Spring WebFlux - Spring Reactive Programming With Examples

Spring WebFlux is the new module introduced in Spring 5. Spring WebFlux is the first step towards reactive programming model in spring framework.

Spring Reactive Programming

If you are new to reactive programming model, then I would highly suggest you to go through following articles to learn about reactive programming.

If you are new to Spring 5, please go through Spring 5 Features.

Spring WebFlux

Spring WebFlux is the alternative to Spring MVC module. Spring WebFlux is used to create fully asynchronous and non-blocking application built on event-loop execution model.

Below diagram from Spring Official Documentation provides great insight on comparison of Spring WebFlux to Spring Web MVC.

spring-mvc-and-webflux

If you are looking to develop a web application or Rest web service on non-blocking reactive model, then you can look into Spring WebFlux.

Spring WebFlux is supported on Tomcat, Jetty, Servlet 3.1+ containers, as well as on non-Servlet runtimes such as Netty and Undertow.

Spring WebFlux is built on Project Reactor. Project Reactor is the implementation of Reactive Streams specification. Reactor provides two types:

  1. Mono: implements Publisher and returns 0 or 1 elements
  2. Flux: implements Publisher and returns N elements.

Spring WebFlux Hello World Example

Let’s built a simple Spring WebFlux Hello World application. We will create a simple rest web service and use Spring Boot to run it on default Netty server.

Our final project structure looks like below image.

spring-webflux-hello-world

Let’s look into each component of the application one by one.

Spring WebFlux Maven Dependencies

The most important dependencies are spring-boot-starter-webflux and spring-boot-starter-parent. Some other dependencies are for creating JUnit test cases.

Spring WebFlux Handler

Spring WebFlux Handler method handles the request and returns Mono or Flux as response.

Notice that reactive component Mono holds the ServerResponse body. Also look at the function chain to set the return content type, response code and body.

Spring WebFlux Router

Router method are used to define routes for the application. These methods return RouterFunction object that also holds ServerResponse body.

So we are exposing a GET method for /helloWorld and the client call should accept plain text response.

Spring Boot Application

Let’s configure our simple WebFlux application with Spring Boot.

If you look at above code, there is nothing related to Spring WebFlux. But Spring Boot will configure our application as Spring WebFlux since we have added dependency of spring-boot-starter-webflux module.

Java 9 Modules Support

Our application is ready to execute on Java 8, but if you are using Java 9 then we also need to add module-info.java class.

Running the Spring WebFlux Spring Boot App

If you have Spring support in Eclipse, then you can run above class as Spring Boot App.

eclipse-run-as-spring-boot-app

If you like to use command line, then open terminal and run command mvn spring-boot:run from the project source directory.

Once the app is running, notice following log messages to make sure everything is good with our app. It’s also helpful when you extend this simple app by adding more routes and functionalities.

It’s clear from logs that our app is running on Netty server on port 8080. Let’s go ahead and test our application.

Spring WebFlux App Test

We can test our app with various methods.

  1. Using CURL command
  2. Launch URL in Browser
  3. spring-webflux-rest-web-service-test
  4. Using WebTestClient from Spring 5Here is a JUnit test program to test our Rest web service using WebTestClient from Spring 5 reactive web.

    Run it a JUnit test case and it should pass with flying colors.

     

  5. eclipse-run-as-spring-boot-app
  6. Using WebClient from Spring Web ReactiveWe can also use WebClient to call the REST web service.

    Just run it as a simple java application and you should see the proper output with a lot of debug messages.

     

  7. spring-reactive-web-WebClient-example

Summary

In this post we learned about Spring WebFlux and how to build a hello world reactive Restful web service.

It’s good to see that popular frameworks such as Spring are rooting for reactive programming model. But we have a lot to cover because if all your dependencies are not reactive and non-blocking then your application is also not truly reactive.

For example, relational database vendors doesn’t have reactive drivers because they depend on JDBC, that is not reactive. Hence Hibernate API is also non-reactive. So if you are using relational databases then you can’t built a truly reactive application, yet. I am hopeful that it will change sooner than later.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: