Not so many people, using Spring MVC consider or even know the alternative way of describing the controllers (or better say handlers) in the project. I would like just quickly show how nice and concise could be this approach, and also, what is not less important, fun. This is the main reason why we all are here, right?
To show something, we need this “something”. Let’s quickly create the simple, sample MVC project in Spring. To do that I am going to use Spring Boot CLI.
let’s open it:
Nothing super interesting yet, the Spring Boot CLI is just the console wrapper for the spring initializr
Func endpoints sample
Now we are ready to go. Let’s create something nice:
Here we have created the
RouterFunction, the main aim of which is to describe the mapping of the request to their appropriate handlers. This
function registered as just a Spring bean. And the
Handler is just the function, which receives the
ServerRequest and returns the
The documentation suggests doing it in slightly different way, but you will understand why I changed it a bit in the next section… And yes, forgive me, that’s groovy again :)
Before proceeding to the next part I would like to notice a few things. Actually what is the difference with the usual approach through the
@RequestMapping annotations? Well, there is less magic here. You create the explicit mapping function routing request to
the right handler. This moves the routing engine from the level of controllers to the level of configuration and brings you a bit more control
over this in a big project. Besides you can disable the scanning for the controllers at all, and most like would get some startup performance improvement.
And last but not least, if you are using more advanced language, then just Java, the handlers could be very dry and self-explanatory - that’s what I am going to try. Let’s rewrite the routing function to groovy and use some of its features.
Nice improvements in Groovy
As you can see, it’s a huge playground for groovy enthusiasts, I just used the simplest example, but I had some fun already.
Just used the basic things:
* arguments in one line with right names of the methods,
* default variable name for the
Closure - it.
And that’s it, but the routing function now looks much more interesting.
Let’s finally rung the code and check how it works:
I am pretty sure, that you could feel a bit skeptical regarding this approach of creating the Controllers in Spring MVC.
But I think, that it deserves at least a try, especially if you are using the synthetically advanced language, which could make this functional nature of this approach even more powerful.