Http://stackoverflow.com/questions/4150039/how-can-i-have-case-insensitive-urls-in-spring-mvc-with-annotated-mappings
The following four URLs can point to the same controller method
Http://localhost:8080/login/userLOgin
Http://localhost:8080/LogiN/userLOgin
Http://localhost:8080/login/userlogin
Http://localhost:8080/Login/UserLogin
Solution One (the following
applicationcontext.xml Configuration Some can be simplified )
1234567 |
public class CaseInsenseticePathMatcher
extends AntPathMatcher {
@Override
protected boolean doMatch(String pattern, String path,
boolean fullMatch, Map<String, String> uriTemplateVariables) {
System.err.println(pattern +
" -- " + path);
return super
.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);
}
}
|
Applicationcontext.xml:
12345678910111213141516171819202122 |
<
bean id
=
"matcher" class
=
"test.CaseInsenseticePathMatcher"
/>
<
bean class
=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
>
<
property name
=
"pathMatcher" ref
=
"matcher"
/>
</
bean
>
<
bean class
=
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
>
<
property name
=
"pathMatcher" ref
=
"matcher"
/>
<
property name
=
"webBindingInitializer"
>
<
bean class
=
"org.springframework.web.bind.support.ConfigurableWebBindingInitializer"
/>
</
property
>
<
property name
=
"messageConverters"
>
<
list
>
<
bean class
=
"org.springframework.http.converter.ByteArrayHttpMessageConverter"
/>
<
bean class
=
"org.springframework.http.converter.StringHttpMessageConverter"
/>
<
bean class
=
"org.springframework.http.converter.FormHttpMessageConverter"
/>
<
bean class
=
"org.springframework.http.converter.xml.SourceHttpMessageConverter"
/>
</
list
>
</
property
>
</
bean
>
<
bean id
=
"conversion-service" class
=
"org.springframework.format.support.FormattingConversionServiceFactoryBean"
/>
|
Simplified applicationcontext.xml: handlermapping, Handleradapter (i.e. controller and action) must be configured
1234567891011 |
<
bean id
=
"matcher" class
=
"com.le.lej.common.CaseInsenseticePathMatcher" />
<
bean
class
=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
>
<
property name
=
"pathMatcher" ref
=
"matcher" />
</
bean
>
<
bean
class
=
"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
>
<
property name
=
"pathMatcher" ref
=
"matcher" />
</
bean
>
|
Second, solution two(need Spring4.2 above support)
123456789 |
@Configuration
public class WebConfig
extends WebMvcConfigurerAdapter {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
AntPathMatcher matcher =
new AntPathMatcher();
matcher.setCaseSensitive(
false
);
configurer.setPathMatcher(matcher);
}
}
|
[email Protected] casing insensitive case insensitive