8

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 ?

4 Answers 4

9

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>
0
1

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;
    }
}
5
  • 1
    I have already solved by replacing entire xml configuration by programmatic java one. But I had also tried this one which did not helped me.
    – Cemo
    Nov 29, 2012 at 14:10
  • Okay, Java config worked for you; Good. If you get time, please updated the post by adding details of your resolution. It will provide heads up for upcoming viewers :)
    – jeevatkm
    Nov 29, 2012 at 14:26
  • 1
    Actually, I have same intention. But my solution is not answer of the question. The real solution must be based on xml configuration.
    – Cemo
    Nov 29, 2012 at 14:36
  • @Cemo Can you explain why this solution didnt work? It seems to be very similar to this solution Mar 20, 2016 at 18:42
  • @AlanBerezin The time when I was asked this question, I was using completely xml based approach for configuring beans. Mixing configurations seems a little bid dangerous for me. Since It can easily trap my colleagues into a pitfall. Nowadays I am avid supporter of Spring Boot and java config. I have completely left xml configurations which are ugly and less type safe when you compared with java config. This answer probably would work now as well.
    – Cemo
    Mar 21, 2016 at 23:19
1

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);
        }
    }
}
1
0

Add the following to your spring configuration file to toggle the useTrailingSlashMatch field.

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useTrailingSlashMatch" value="true">
    </property>
</bean>
1
  • 4
    I have already tried this. mvc:annotation-driven is creating another RequestMappingHandlerMapping. I have stated on my question it.
    – Cemo
    Nov 27, 2012 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.