Pretty URL in Spring

Spring Framework's support for pretty (aka meaningful, restful) URLs was rather weak so I've been hacking together a HandlerMapping implementation and I thought others might find its design useful.

RestfulServletHandlerMapping class currently routes URLs to Controller implementations by configuring properties like this:

<property name="urlParameterPattern" value="\{([\w-_\.]+)\}" />

<property name="urlParameters">


    <entry key="" value="[a-zA-Z][\w-]*" />

    <entry key="post.year" value="20\d\d" />

    <entry key="post.month" value="[01]\d" />

    <entry key="" value="[\w-]+" />



<property name="urlMappings">




       value="blogController" />



Some explanation of the properties:

urlParameterPattern – regular expression used to identify named parameters in urlMappings URL patterns.

urlParameters – collection of named parameters and format specified using regular expression.

urlMappings – collection of URL patterns expressed as regular expressions with named parameters.

Currently, RestfulServletHandlerMapping builds a composite regular expression pattern for each urlMappings entry by replacing named parameters with the parameter's format regular expression. When a request comes in, request URL is compared against each pattern until a match is found. When a match is found, parameter values are stored as request parameters using the parameter name then control is passed to the controller mapped to the URL pattern.

While this design is pretty flexible, it has two shortcomings:

  1. non-ASCII parameters is, while possible, tedious to define.
  2. semantic parameter validation support (is 'donpark' a real user?)

Above shortcomings can be easily avoided by carefully arranging URLs and order of evaluation though.