Spring MVC @RequestMapping注释的不区分大小写映射

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

我有控制器在其中有多个@RequestMapping注释。

@Controller public class SignUpController { @RequestMapping("signup") public String showSignUp() throws Exception { return "somejsp"; } @RequestMapping("fullSignup") public String showFullSignUp() throws Exception { return "anotherjsp"; } @RequestMapping("signup/createAccount") public String createAccount() throws Exception { return "anyjsp"; } } 

如何将这些@RequestMapping映射到不区分大小写。 即如果我使用“/ fullsignup”或“/ fullSignup”,我应该得到“anotherjsp”。 但现在这种情况并没有发生。 只有“/ fullSignup”才能正常工作。

我试过扩展RequestMappingHandlerMapping但没有成功。 我也尝试过AntPathMatcher,就像在这个论坛上提到的另一个问题,但它也不适用于@RequestMapping注释。

调试控制台 在此处输入图像描述

服务器启动时的输出控制台。

在此处输入图像描述

我添加了两张显示问题的图片。 我已经尝试了下面提到的两种解决方案。 控制台说它映射了小写的URL,但是当我请求访问带有小写url的方法时,它表明存储值的原始映射包含MixCase URLS。

如何在Spring MVC中使用带注释的映射中具有不区分大小写的URL的方法之一可以完美地工作。 我只是在控制器和请求方法的水平上使用@RequestMapping的组合尝试它并且它干净地工作,我只是在这里为Spring 3.1.2复制它:

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 uriTemplateVariables) { return super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables); } } 

使用Spring MVC注册此路径匹配器,删除注释,并替换为以下内容,进行适当配置:

                               

或者使用@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; } } 

以下简单的解决方案应该使@RequestMapping不敏感,无论它是注释Controller还是方法。 Biju的解决方案也应该有效。

创建此自定义HandlerMapping:

 public CaseInsensitiveAnnotationHandlerMapping extends DefaultAnnotationHandlerMapping { @Override protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception { return super.lookupHandler(urlPath.toLowerCase(), request); } @Override protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException { super.registerHandler(urlPath.toLowerCase(), handler); } } 

并在[servlet-name] -servlet.xml中添加:

  

注意:如果您不希望在应用程序中使用两个HandlerMapping,则可能需要删除 (它实例化DefaultAnnotationHandlerMapping )。