如何在带有注释映射的Spring MVC中使用不区分大小写的URL

Dav*_*ave 33 java mapping spring-mvc case-insensitive

通过我的spring mvc web应用程序,我有注释的映射工作,但是,它们区分大小写.我找不到让它们不区分大小写的方法.(我很乐意在Spring MVC中实现这一点,而不是以某种方式重定向流量)

npc*_*ode 23

Spring 4.2将支持不区分大小写的路径匹配.您可以按如下方式配置它:

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在 Spring Boot 2 或 Spring 5 中,不推荐使用`WebMvcConfigurerAdapter`。相反,应该直接实现`WebMvcConfigurer`。 (4认同)

sma*_*mat 13

根据这个webpost,您需要在Spring MVC中添加HandlerMappingHandlerAdapter.Mapping将请求映射到相应的控制器,适配器负责使用控制器执行请求.

因此,您需要覆盖映射器和适配器的PathMatcher.

Ex(将使所有@Controllers不区分大小写):

新匹配:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

applicationContext.xml中:

<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"/>
Run Code Online (Sandbox Code Playgroud)

添加了与< mvc:annotation-driven > 相同的内容.(感谢David Parks的链接)

  • 对于Spring 3.1,使用RequestMappingHandlerAdapter替换AnnotationMethodHandlerAdapter.他们在3.1中使用这个新类. (3认同)

pcz*_*eus 6

在 Spring 3.2+ / Spring Boot 中,您现在可以使用简化的 Java 配置设置不区分大小写的 URL 匹配。

首先,您需要创建 CaseInsensitivePathMatcher.groovy 或 Java 类:

import org.springframework.util.AntPathMatcher

class CaseInsensitivePathMatcher extends AntPathMatcher{

    @Override
    protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) {
        super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables)
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,要实现这一点,您应该有一个用 Springs @Configuration 注释的类,它扩展了 WebMvcConfigurerAdapter 类,如下所示(请注意,我的代码包含在 .groovy 类中,因此示例中不需要 'return' 关键字) :

@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter
Run Code Online (Sandbox Code Playgroud)

然后将以下2个方法添加到类中:

/**
 * Creates a patchMatcher bean that matches case insensitively
 * @return PathMatcher
 */
@Bean
public PathMatcher pathMatcher() {
    new CaseInsensitivePathMatcher()
}

/**
 * Overrides the configurePathMatch() method in WebMvcConfigurerAdapter
 * <br/>Allows us to set a custom path matcher, used by the MVC for @RequestMapping's
     * @param configurer
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.pathMatcher = pathMatcher()
    }
}
Run Code Online (Sandbox Code Playgroud)


就是这样,您现在应该以最少的配置为不区分大小写的 URL 进行所有设置


Dav*_*rks 0

好吧,我无法回答你的问题(我试过了,我想我能弄清楚)。但鉴于您已经两天没有收到任何回复,至少有一些线索:

这个例子似乎表明这是可能的:

http://webcache.googleusercontent.com/search?q=cache:ELj-ZQ8G4z0J:www.springbyexample.org/examples/sdms-simple-spring-mvc-web-module.html+case+insensitive+requestmapping+spring&cd= 3&hl=en&ct=clnk&client=firefox-a

它在 Spring 中引用了这个类

http://static.springsource.org/spring/docs/3.0.4.RELEASE/javadoc-api/org/springframework/web/servlet/mvc/support/ControllerClassNameHandlerMapping.html

我的猜测(只是猜测)是,您需要使用<mvc:annotation-driven/>正确的参数来扩展和实现各个 bean,以使其不区分大小写。看:

http://rapid-web.tumblr.com/post/296916668/what-does-annotation-driven-do

最后一点,我在阅读其他地方时注意到它说所有路径默认为小写,您是否已验证这/MyPath不是由 处理的@RequestMapping("/mypath")

再说一次,我只是尽我所能做一些深思熟虑的事情。也许它会让你走得足够远,提出一个更具体的问题,引导你找到答案——有时这些事情就是这样运作的。祝你好运!