Java 1.8 brought functional programming into the java world and now the libraries and frameworks that are based on Java are responding to it.
Today we are going to discuss the new functional web framework that Spring Framework 5.0 introduced and why I think it to be beautiful.
Before we begin, we can look how a statement is different from expression; in the world of programming.
Statements are rude, we tell the compiler (and the colleagues that are reading the code) that I'm doing this operation. While it is better than not leaving a clue whats happening in the code, there are better ways of communication.
On the other hand, expressions are sweeter, they express the code when properly used.
The web technologies and integration have changed a lot over the years, in the last 5 or 6 years there has been a lot of restful services around. The smooth Integration with the front end and the increase in the popularity of microservices might have made restful services more popular.
In this blog, we will be looking into the rest service part of the spring 5 functional web API and how it is different from the spring MVC web services.
The functional style of programming is often considered to be more elegant than the imperative style or the declarative style. When I tried writing web services for the crud operations of an entity, for evaluation purposes I found that the spring functional API can express your restful service endpoints in an elegant way.
If I have a repository of Movies and a service class to access the repositories the rest service that I would write using spring MVC might look like the below.

If the above code is refactored using Spring Framework 5.0 functional web framework, it would look like the below.

If I have a repository of Movies and a service class to access the repositories the rest service that I would write using spring MVC might look like the below.
If the above code is refactored using Spring Framework 5.0 functional web framework, it would look like the below.
And we can have the handler functions outside the RouterFunction as below.
Never get flattered by the looks. No one is going to afford beautiful code if it's not yielding good performance.
Never get flattered by the looks. No one is going to afford beautiful code if it's not yielding good performance.
Spring Framework 5.0 Functional web API flatters us not only with the look, under the hood, it uses the reactivate foundation which is super good to work with a large amount of data and it's really handy when you are processing a continuous flow of data as well.
Mono and Flux
Mono and Flux are the types that Spring Web Reactor is providing. Mono means that you are dealing with 0 or 1 number of items. On the other hand, Flux means that you are dealing with 0 or n number of items. Mono is for the processing of single elements and Flux is for the processing of a stream of elements. They allow providing a consistent API for handling single and multiple elements in a Reactive way.
HandlerFunction
HandlerFunction is the starting point of the new web framework. This is a Java-8 'Function' that can process a 'Request' and 'Response'.HandlerFunctions are fully reactive by building on top of Reactor
RouterFunction
Incoming requests are routed to handler functions with a RouterFunction.
A router function evaluates to a handler function if it matches; otherwise, it returns an empty result.
The RouterFunction has a similar purpose as a @RequestMapping annotation. However, there is an important distinction: with the annotation, your route is limited to what can be expressed through the annotation values, and the processing of those is not trivial to override; with router functions, the processing code is right in front of you: you can override or replace it quite easily.
Thanks to Spring Engineering team to put together this beautiful piece of web API for the Java community
Mono and Flux
Mono and Flux are the types that Spring Web Reactor is providing. Mono means that you are dealing with 0 or 1 number of items. On the other hand, Flux means that you are dealing with 0 or n number of items. Mono is for the processing of single elements and Flux is for the processing of a stream of elements. They allow providing a consistent API for handling single and multiple elements in a Reactive way.
HandlerFunction
HandlerFunction is the starting point of the new web framework. This is a Java-8 'Function' that can process a 'Request' and 'Response'.HandlerFunctions are fully reactive by building on top of Reactor
RouterFunction
Incoming requests are routed to handler functions with a RouterFunction.
A router function evaluates to a handler function if it matches; otherwise, it returns an empty result.
The RouterFunction has a similar purpose as a @RequestMapping annotation. However, there is an important distinction: with the annotation, your route is limited to what can be expressed through the annotation values, and the processing of those is not trivial to override; with router functions, the processing code is right in front of you: you can override or replace it quite easily.
Thanks to Spring Engineering team to put together this beautiful piece of web API for the Java community
Comments
Post a Comment