Spring MVC @RequestMapping 注释不区分大小写映射 [英] case insensitive mapping for Spring MVC @RequestMapping annotations
问题描述
可能的重复:
完美地工作.我只是在控制器和请求方法级别结合@RequestMapping 进行了尝试,并且运行良好,我只是在此处为 Spring 3.1.2 重现它:
CaseInsensitivePathMatcher:
import java.util.Map;导入 org.springframework.util.AntPathMatcher;公共类 CaseInsensitivePathMatcher 扩展 AntPathMatcher {@覆盖protected boolean doMatch(String pattern, String path, boolean fullMatch, Map uriTemplateVariables) {返回 super.doMatch(pattern.toLowerCase(), path.toLowerCase(), fullMatch, uriTemplateVariables);}}
用Spring MVC注册这个路径匹配器,去掉<mvc:annotation-driven/>
注解,替换成如下,适当配置:
<属性名称=webBindingInitializer"><bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"><property name="conversionService" ref="conversionService"></property><属性名称=验证器"><bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"><property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property></bean></属性></bean></属性><属性名称="messageConverters"><列表><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></属性></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>
或者使用@Configuration 更轻松、更干净:
@Configuration@ComponentScan(basePackages="org.bk.webtestuuid")公共类 WebConfiguration 扩展了 WebMvcConfigurationSupport{@豆角,扁豆公共路径匹配器路径匹配器(){返回新的 CaseInsensitivePathMatcher();}@豆角,扁豆公共 RequestMappingHandlerMapping requestMappingHandlerMapping() {RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();handlerMapping.setOrder(0);handlerMapping.setInterceptors(getInterceptors());handlerMapping.setPathMatcher(pathMatcher());返回处理程序映射;}}
Possible Duplicate:
How can I have case insensitive URLS in Spring MVC with annotated mappings
I have Controller having multiple @RequestMapping annotations in it.
@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";
}
}
How can I map these @RequestMapping to case insensitive. i.e. if I use "/fullsignup" or "/fullSignup" I should get "anotherjsp". But this is not happening right now. Only "/fullSignup" is working fine.
I've tried extending RequestMappingHandlerMapping but no success. I've also tried AntPathMatcher like the guy mentioned there is another question on this forum but its also not working for @RequestMapping annotation.
Debugging console
Output console when server is up.
I've added two images which shows the problem. I've tried both the solutions mentioned below. The console says that it mapped lowercased URLS but when I request to access a method with lowercase url then it shows that the original map where the values are stored stilled contained MixCase URLS.
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 MVC @RequestMapping 注释不区分大小写映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!