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.
@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.
The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method. The @RequestParam annotation can be used with or without a value.
One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.
Short answer: There is no difference in semantic. Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET).
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;
}
}
The following simple solution should make @RequestMapping insensitive, whether it annotates a Controller or a method. Biju's solution should work too.
Create this custom 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);
}
}
And add this in your [servlet-name]-servlet.xml :
<bean class="yourpackage.CaseInsensitiveAnnotationHandlerMapping" />
Note: if you don't want two HandlerMapping in your app, you may want to remove <mvc:annotation-driven />
(it instantiates a DefaultAnnotationHandlerMapping
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With