Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Spring MVC's behavior in handling url 'dot' character

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"/>
like image 687
Alexey Buistov Avatar asked Nov 09 '10 15:11

Alexey Buistov


3 Answers

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>
like image 104
AHungerArtist Avatar answered Oct 13 '22 01:10

AHungerArtist


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.

like image 31
Josh Avatar answered Oct 12 '22 23:10

Josh


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.

like image 34
Ryba Avatar answered Oct 12 '22 23:10

Ryba