How to Have Case Insensitive Urls in Spring MVC with Annotated Mappings

case insensitive mapping for Spring MVC @RequestMapping annotations

One of the approaches in How can I have case insensitive URLS in Spring MVC with annotated mappings works perfectly. I just tried it with combinations of @RequestMapping at the level of controller and request methods and it has worked cleanly, I am just reproducing it here for Spring 3.1.2:

The CaseInsensitivePathMatcher:

import java.util.Map;

import org.springframework.util.AntPathMatcher;

public class CaseInsensitivePathMatcher extends AntPathMatcher {
@Override
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);
}
}

Registering this path matcher with Spring MVC, remove the <mvc:annotation-driven/> annotation, and replace with the following, configure appropriately:

<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService"></property>
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
</bean>
</property>
</bean>
</property>
<property name="messageConverters">
<list>
<ref bean="byteArrayConverter"/>
<ref bean="jaxbConverter"/>
<ref bean="jsonConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
</list>
</property>
</bean>
<bean name="byteArrayConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean name="jaxbConverter" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean name="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean name="caseInsensitivePathMatcher" class="org.bk.lmt.web.spring.CaseInsensitivePathMatcher"/>
<bean name="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="pathMatcher" ref="caseInsensitivePathMatcher"></property>
</bean>

Or even more easily and cleanly using @Configuration:

@Configuration
@ComponentScan(basePackages="org.bk.webtestuuid")
public class WebConfiguration extends WebMvcConfigurationSupport{

@Bean
public PathMatcher pathMatcher(){
return new CaseInsensitivePathMatcher();
}
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setInterceptors(getInterceptors());
handlerMapping.setPathMatcher(pathMatcher());
return handlerMapping;
}
}

Spring Boot 2.6.7 URL case insensitive configuration

After @Xiidref solution could you add below.

(If you are using spring-boot, just add this in your property and it will work)

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

in your application.properties file.

Spring MVC case insensitive URLs

The default matching mechanism for the SimpleUrlHandlerMapping is an AntPathMatcher. You could either create your own PathMatcher implementation or subclass the AntPathMatcher and set this back on the SimpleUrlHandlerMapping.

The PathMatcher interface is fairly straight forward to implement.

public class CaseInsensitiveAntPathMatcher extends AntPathMatcher {

@Override
public boolean match(String pattern, String string) {
return super.match(pattern.toLowerCase(), string.toLowerCase()); // make this according to your need
}
}

In the configuration it looks like this:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key=".htm">pageController</prop>
<prop key=".html">pageController</prop>
<prop key="/cms/*">pageController</prop>
<prop key="/admin/*">adminController</prop>
</props>
</property>
<property name="pathMatcher">
<bean class="packagename.CaseInsensitiveAntPathMatcher"/>
</property>
<property name="alwaysUseFullPath" value="true"/>


Related Topics



Leave a reply



Submit