Spring 2.5 with annotations and REST requests
Spring 2.5 Web MVC is a really great framework, especially when using annotations you don’t have to spend to much time on getting everything up and running and separating MVC logic is really simple. One problem I recently came across, was something which was a bit annoying. When using Spring 2.5 with annotations, it’s not possible to define a URL pattern with a wild card in it. So creating REST full URL patterns can be a problem then. This is something that they will have build into Spring 3.0. But since that is not ready for production I wrote a small hack to at least be able to easily define URL patterns with wild cards.
Creating a custom handler mapping Object
The first thing to do is, is to create a custom handler mapping Object. This Object can extend the Spring DefaultAnnotationHandlerMapping. This handler mapping won’t overwrite the DefaultAnnotationHandlerMapping. But we will just use this one to test for our REST url patterns. If no match is found. Then the method getHandlerInternal will return null. When null is returned, this means that Spring will go on and try to do this request in any of the other handler mappings configured. How this is configured in Spring comes up a little bit later in this post.
The code for the new RestHandlerMapping is as follows:
package com.radikalfx.demo.handler; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Required; import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping; public class RestHandlerMapping extends DefaultAnnotationHandlerMapping { private String[] requestNames; @Override public HandlerExecutionChain getHandlerInternal(HttpServletRequest request) throws Exception { String uri = request.getRequestURI(); for (String requestName : requestNames) { if (uri.startsWith(requestName)) { return new HandlerExecutionChain(this.getHandlerMap().get(requestName)); } } return null; } @Required public void setRequestNames(String[] requestNames) { this.requestNames = requestNames; } }
This RestHandlerMapping Object has the option to set one or more request names. This was we can just have this one Handler to set up multiple different URL patterns for different Controllers.
The Controller
Next is the controller. There is not much special that needs to happen to the controller. The only thing that we need to do, is make sure that we place a request mapping similar to that provided to the handler. And that this request mapping is placed on the class and not on the method.
package com.radikalfx.demo.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/action/") public class ActionController { @RequestMapping public ModelAndView getAction(HttpServletRequest request) { return new ModelAndView("action"); } }
Spring configuration
Next thing we need to do, is that we need to change the Spring configuration a little bit. Normally when using annotated Web MVC you would probably use the DefaultAnnotationHandlerMapping, with this hack, we are still doing that. But creating an own handler mapping to be used before this one. In you configuration, this would become:
<bean class="com.radikalfx.demo.handler.RestHandlerMapping"> <property name="requestNames"> <list> <value>/action/</value> </list> </property> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
Don’t forget to also include in the Controller in the Spring configuration!
Changing web.xml
The final thing that you need to do, to get this working, is change the web.xml so that the DispatcherServlet gets the requests for the URL pattern that we want to use:
<url-pattern>/action/*</url-pattern>
And that’s it. Of course you probably want to have some service that can handle the HttpServletRequest in the controller and extract the proper information from the request URI. But basically this is all you need to use wild cards in URL patterns with Spring 2.5 and annotations.

Very informative and will be sure to come again.
Always great to stumble across a new website this fantastic! I will be back for certain!
Code above should have been like this to support context-aware deployment
String uri = request.getRequestURI();
if(request.getContextPath() != null && request.getContextPath().length()>0){
uri = uri.substring(request.getContextPath().length());
}
Thanks, works like a charm! Exactly what I was looking for.