Reputation: 12861
In order to implement our solution for versioning of request mappings I implemented my own RequestMappingHandlerMapping
as a subclass of it. I overrode registerHandlerMethod()
and lookupHandlerMethod()
. The key concept is that one request mapping can appear multiple times, but the various implementations are stored in different Java-packages. My new class picks the required version from the correct package based on the first path element of the servlet path, which is the version number. The version is removed before looking for the proper request mapping handler.
This is how I weave it into Spring:
import javax.inject.Inject;
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 VersioningWebMvcConfigurationSupport extends WebMvcConfigurationSupport {
private VersioningRequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
requestMappingHandlerMapping.setOrder(0);
requestMappingHandlerMapping.setInterceptors(getInterceptors());
return requestMappingHandlerMapping;
}
@Inject
public void setRequestMappingHandlerMapping(VersioningRequestMappingHandlerMapping requestMappingHandlerMapping) {
this.requestMappingHandlerMapping = requestMappingHandlerMapping;
}
}
The IMHO important bits of my applicationContext.xml:
<context:annotation-config />
<context:mbean-export />
<context:component-scan base-package="..." />
<mvc:annotation-driven />
Let me know if you want me to look for some other lines there.
My problem is that my code runs through correctly, duplicates are handled properly. However, the original (?) RequestMappingHandlerMapping
still gets initialised. That one fails due to the duplicate request mappings.
How can I avoid this? The way I see it, my web-app would run nicely without the default RequestMappingHandlerMapping
.
Upvotes: 3
Views: 2907
Reputation: 8601
Not very sure, but the tag <mvc:annotation-driven />
might be registering the RequestMappingHandlerMapping
in your webapp.
Spring MVC documentation mention that RequestMappingHandlerMapping is by default enabled if you using MVC namespace:
Spring 3.1 introduced a new set of support classes for @RequestMapping methods called RequestMappingHandlerMapping and RequestMappingHandlerAdapter respectively. They are recommended for use and even required to take advantage of new features in Spring MVC 3.1 and going forward. The new support classes are enabled by default from the MVC namespace and with use of the MVC Java config but must be configured explicitly if using neither.
Source: documentation
So I suspect you have to get rid of that <mvc:annotation-driven />
tag in order to disable the default behavior of RequestMappingHandlerMapping
.
Upvotes: 4