First8 is gespecialiseerd in het pragmatisch ontwikkelen van bedrijfskritische Java toepassingen waarbij integratie van systemen, hoge eisen aan beveiliging en veel transacties een belangrijke rol spelen. Op deze pagina vindt je onze blogs. Op www.first8.nl vind je ons nieuws, de agenda, vacatures en meer.

Spring WebFlux functional based

In my previous blog about WebFlux ‘Spring WebFlux annotation based‘, I discussed the annotation based reactive programming model for Spring WebFlux. In this blog I will use the example project introduced then and adapt it to the functional programming model.

The example project we have serves the names of planets in our solar system, optionally applying a filter, in plain text. Using curl on the running application gives:

The Spring WebFlux annotation based controller we are adapting looks like this:

The functional model of WebFlux is quite different from the annotation based model. Where the annotation based model builds upon the already familiar @Controller,@RequestMapping and other web annotations, the functional model sets these aside and introduces concept of a RouterFunction and HandlerFunction.

RouterFunction

Router functions are the functional counterpart of the @RequestMapping annotation and used to match incomming requests to handler functions. A router function takes a ServletRequest as input and returns the appropriate handler function. The signature of a router function is as follows:

The router function definition for the @RequestMapping(path={"/planets/{substring}","/planets"}) annotation looks like this:

HandlerFunction

Handler functions contain the actual business logic for processing the request. The signature of a handler function is as follows:

The handler function implementation does not differ that much from the controller implementation, except that we now have to deal with a ServerRequest object that is our starting point for processing the request. The ServerRequest offers us reactive style access to request information. Transforming the controller logic to handler logic gives the following:

There you go a fully reactive web application using the functional model of WebFlux. The full source code for the project can be found on github

This is a cross-post from my personal blog. Follow me on @acuriouscoder if you like what you’re reading or subscribe to my blog on https://acuriouscoder.net.