Functional Programming with Spring WebFlux

Spring framework version 5 with support from Java 8 and above, can help us using Functional Programming in Java code. And Spring WebFlux is no exception. We can also build Reactive web applications using Lambda Expression. In this tutorial, I will guide you all how to use Functional Programming with Spring WebFlux.

First, I will also create a new Spring Boot project with Reactive Web support using Spring Initializr Web as an example:

Functional Programming with Spring WebFlux

If you do not know how to create Spring Boot project with Spring Initializr Web, you can refer to this tutorial.

Note that, after creating the project, you should open the Maven pom.xml file and remove the dependency spring-boot-starter-web:

Otherwise we will not run the example.

As in the tutorial about Spring WebFlux using annotation, in this tutorial, I will also create an application that provides list of student with data added every second. When a user requests to our application, whenever a new student is added, the student’s information is published to the user.

Student information will be stored in the Student object as follows:

Here, I also used Project Lombok.

For Functional Programming in Spring WebFlux, instead of using annotations in Spring MVC, we will now use the HandlerFunction with Lambda Expression to process the request and map request URL to the HandlerFunction object using the RouterFunction object.

Here, we can see the RouterFunction like the @RequestMapping annotation that defines the request URL, the HTTP method, and so on. HandlerFunction is the method declared with the @RequestMapping annotation in Spring MVC.

  • The HandlerFunction is a Functional Interface that will process the request from the user from the ServerRequest object and return the client object ServerResponse.

The content of this interface is as follows:

Here, the ServerRequest and ServerResponse objects are new objects introduced with Spring WebFlux to hold information about the request and response of a request URL in Reactive web applications. We can get Mono or Flux objects in Project Reactor from these objects.

In the example of this tutorial, I will create a HandlerFunction with the following contents:

The ServerResponse.ok () method means: we will return the result to the client with HTTP status code of 200. Here, we also use the Content-Type “text/event-stream” so that whenever new data is available, the server will update that data for the client.

The body() method defines the data returned to the client. In this method, we also define the all() method to generate student information after a second time as follows:

With the RandomStringGenerator object used from the Apache Commons Text library:

  • RouterFunction is also a Functional Interface and it maps the request URL to a HandlerFunction that uses the RequestPredicate object to process the request from the user.

The contents of the RouterFunction interface are as follows:

We can use the static methods of the RequestPredicates object to create a RequestPredicate object:

and the static methods of the RouterFunctions object to create the RouterFunction object from the RequestPredicate and HandlerFunction objects above:

The entire code for creating the request URL is as follows:

If using Lambda Expression, the code can be written as follows:

To register this URL request with Spring WebFux, we need to declare the RouterFunction in the Spring container.

For simplicity, we will declare the bean for the RouterFunction object in the SpringWebfluxFunctionalApplication file. The contents of this file will now look like this:

That is it, let’s try to run it.

You will also see a new student added in a second and returned to the user as follows:

Functional Programming with Spring WebFlux


Add Comment