6.5.2 Groovy as routing configuration syntax
The configuration syntax for the routing is not a plain data file like XML, but rather an executable program in the form of a Groovy script. Groovy has a standard pattern for describing tree-structures in an easy-to-read manner, which will look like a plain data syntax unless you start making use of more advanced features. This way of using an existing programming language in such a way that it looks like a specialized syntax is called an internal DSL.
Groovy as syntax: pro and con
Some of the pro's of using Groovy as syntax are:
- It is no XML: the syntax is 'lighter', e.g. braces are used instead of verbose open and close tags.
- More flexibility: the script can contain anything as long as the result of its execution is a Restlet instance.
- It allows to embed inline request handlers or filters, which can be useful in some simple cases.
To be fair, some cons are:
- As it offers all freedom of a programming language, it enforces less discipline but allows for more hacking. In other words, the power should be used wisely.
- It is code instead of data: it is not read but executed.
- The error reporting is the standard Groovy error reporting.
You might have heard that Groovy is slow and hence naively think that doing the routing in Groovy will introduce a bottleneck in the system. This is a false reasoning, since the Groovy script is executed only once and serves to create the internal routing structure. It is really just an alternative for other e.g. XML syntax. The internal routing structure doesn't make use of Groovy and could be built up by other means as well.