We use Parameterized REST URLs with Spring MVC in many of our projects. In our latest project we used Spring 2.5 annotations.
We did run in problems using the method level annotation.
For example we have a method signature like this:
@RequestMapping(value = "/tags/(*:tagName)/(*:page)")
public String getDetailsByTagName(ModelMap modelMap, @RequestParam("tagName") String tagName, RequestParam("page") int page) {
Urls like “/tags/(*:tagName)/(*:page)” works without any problem if we place the RequestMapping tag on a type level but it did not work on a method level. The problem was that Spring 2.5 installs a set of deault handlers and in specific the AnnotationMethodHandlerAdapter. However the AnnotationMethodHandlerAdapter does use the AntPathMatcher that of course can not handle parameterized urls like “/tags/(*:tagName)/(*:page)”. The solution is simple:
Create a object like this:
public class ParameterizedAnnotationMethodHandlerAdapter extends AnnotationMethodHandlerAdapter {
public ParameterizedAnnotationMethodHandlerAdapter() {
setPathMatcher(new ParameterizedPathMatcher());
} }
Now you need to install this handler by simply create this bean in your spring context.xml file. But this has a little side effect. Spring now will not install the other HandlerAdapters in case you install one manually, so just install all handlers like this:
<!-- we need to overwrite the pathmatcher in AnnotationMethodHandlerAdapter, there for we need to install all handlers manually --> <bean class="com.IOItec.urags.util.spring.url.ParameterizedAnnotationMethodHandlerAdapter"/> <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <bean class="org.springframework.web.servlet.mvc.throwaway.ThrowawayControllerHandlerAdapter"/>
Hope that helps..




No comments yet
Comments feed for this article