Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customizing spring 3 mvc:annotation for RequestMappingHandlerMapping

I am using <mvc:annotation-driven/> and I would like to configure RequestMappingHandlerMapping for disabling useTrailingSlashMatch. When I declare another RequestMappingHandlerMapping, I will end up 2 RequestMappingHandlerMapping. How can I configure RequestMappingHandlerMapping ?

like image 631
Cemo Avatar asked Nov 27 '12 09:11

Cemo


People also ask

What is RequestMappingHandlerMapping?

The RequestMappingHandlerMapping is used to maintain the mapping of the request URI to the handler.

What is handler method annotation in Spring MVC?

annotation. RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods. Today we will look into various usage of this annotation with example and other annotations @PathVariable and @RequestParam .

What is view resolver in Spring MVC?

The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.

Which annotation is required to receive a model object initialized by the request parameters in a request processing method?

The @RequestParam annotation is used to bind request parameters to a method parameter in your controller.


3 Answers

As you have already noted, this is feasible in xml by removing mvc:annotation-driven and replacing with the entire xml equivalent:

<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="true"></property>
</bean>
like image 159
Biju Kunjummen Avatar answered Nov 12 '22 01:11

Biju Kunjummen


Can you try with Java config to override RequestMappingHandlerMapping value

@Configuration
@ComponentScan(basePackages = "base.package.name")
public class WebAppConfig extends WebMvcConfigurationSupport {

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping rmh = super.requestMappingHandlerMapping();
        rmh.setUseTrailingSlashMatch(false);
        return rmh;
    }
}
like image 21
jeevatkm Avatar answered Nov 12 '22 01:11

jeevatkm


If you want a solution that doesn't involve duplicating functionality in Spring then you can override the DisplatcherServlet. in Servlet 3.0 container this might look like:

@WebServlet(name="spring-dispatcher", loadOnStartup=1, urlPatterns={"/"},
        initParams={
            @WebInitParam(name="contextConfigLocation", 
            value="/WEB-INF/spring/spring-dispatcher-servlet.xml")})
public class MyDispatcherServlet extends DispatcherServlet {

    @Override
    protected void initStrategies(ApplicationContext context) {
        super.initStrategies(context);
        for (RequestMappingInfoHandlerMapping handlerMapping 
                : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                    context, RequestMappingInfoHandlerMapping.class, true, false).values()) {

            handlerMapping.setUseTrailingSlashMatch(false);
        }
    }
}
like image 1
scarba05 Avatar answered Nov 11 '22 23:11

scarba05