Spring MVC – Customizing RequestMappingHandlerMapping

When Spring MVC is configured using <mvc:annotation-driven/> in an xml bean definition file, internally a component called RequestMappingHandlerMapping gets registered with Spring MVC. This component or in general a HandlerMapping component is responsible for routing request URI’s to handlers which are the controller methods annotated with @RequestMapping annotation.
 
 
 
 
 
 
 
There are two specific configurations in RequestMappingHandlerMapping that may be non-intuitive:

The first configuration is ‘useSuffixPatternMatch’, which is if say a uri ‘/members’ maps to method to return a list of entities, ‘/members.xyz’ would map to the same handler method.
The second configuration is ‘useTrailingSlashMatch’ which is that ‘/members’ and ‘/members/’ would map to the same handler method.
If these two behavior need to be modified, the way to do that is to configure the RequestMappingHandlerMapping.

If <mvc:annotation-driven/> has been used to define the RequestMappingHandlerMapping, then the unwieldy way to go about it would be to remove <mvc:annotation-driven/> and instead to configure Spring MVC by expanding out the components that get registered by the custom namespace handler handling the ‘mvc’ namespace, which is something along these lines(this is not complete and is being shown just to demonstrate the complexity of the configuration):

<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>
  </bean>
 </property>
 <property name='messageConverters'>
  <list>
   <bean class='org.springframework.http.converter.ByteArrayHttpMessageConverter'/>
   <bean class='org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter'/>
   <bean class='org.springframework.http.converter.json.MappingJackson2HttpMessageConverter'/>
   <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='handlerMapping' class='org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping'>
 <property name='useTrailingSlashMatch' value='false'></property>
</bean>

This is definitely not a good way to go about changing the configuration. Instead if faced with the need to configure RequestMappingHandlerMapping a far better approach is to move a part or the entire web-mvc configuration to Java @Configuration this way:

package mvcsample.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Configuration
public class WebConfig extends WebMvcConfigurationSupport{
 @Bean
 public RequestMappingHandlerMapping requestMappingHandlerMapping() {
  RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
  handlerMapping.setUseSuffixPatternMatch(false);
  handlerMapping.setUseTrailingSlashMatch(false);
  return handlerMapping;
 } 
}

and to import this configuration into the rest of the configuration xml(there are other documented ways also):

<bean class='mvcsample.spring.WebConfig'/>

<!-- 
<mvc:annotation-driven>
</mvc:annotation-driven> 
-->

@Configuration thus provides a simpler mechanism to configure the components in Spring MVC and migration from xml based configuration to Java configuration is highly recommended to simplify the configuration and manage customizations.
 

Reference: Spring MVC – Customizing RequestMappingHandlerMapping from our JCG partner Biju Kunjummen at the all and sundry blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


+ nine = 12



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close