springmvc_@requestmapping casing insensitive Case insensitive

Source: Internet
Author: User

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 @Configurationpublic 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.