I'm trying to migrate a web project off from Jersey to Spring MVC 3.0. The process was really straightforward up to the moment when I started to migrate the controllers supposed to handle URL's with dot notations: "/myApp/resources/create/root.subFolder1". Spring MVC seems to shamelessly cut the ".subFolder1" part from the URL, which happens deep inside framework code (see AbstractUrlHandlerMapping class)
uriTemplateVariables.putAll(getPathMatcher().extractUriTemplateVariables(matchingPattern, urlPath));
So my controller method gets invoked with root path parameter, not root.subFolder1
I'd really like to find a way to customize this behavior. Any advices?
PS. The requirement is kinda to keep the existing URL structure, i.e. workarounds like switching to query params "/myApp/resources/create/?path=root.subFolder1" I cannot consider.
PS. My Spring config looks like
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
<context:component-scan base-package="my.app.pkg"/>
Try this:
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
This should make it so that Spring won't try to parse extensions.
See also: Spring MVC @PathVariable getting truncated
And: Spring Documentation
Potential config files:
web.xml (where I set the servlet stuff)
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml,classpath*:applicationContext.xml,classpath*:restApplicationContext.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
And then in WEB/INF/spring/mvc-config.xml I have something like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
</beans>
Another possibility which may be easier in some circumstances is to add a trailing /
to the end of the URL.
So your URL would be /myApp/resources/create/root.subFolder1/
This works in my app using Spring MVC 3.1.
In order for
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
to work you have to remove/comment out the
<mvc:annotation-driven />
<mvc:default-servlet-handler />
and any other mvc: configurations defined as they override your custom bean declarations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With