I have two methods in my application that use the same request mapping with different (http) methods
@RequestMapping(value = "/method/{key}", method = RequestMethod.GET)
public void method1(<parameters>) throws IOException {
// ...
}
@RequestMapping(value = "/method/{key}", method = RequestMethod.PUT)
public void method2(<parameters>) throws IOException {
// ...
}
This works perfectly as long as both methods are defined in the same Controller, however, because of a company framework I’m using, I need for them to be in 2 different controllers, and when I start my application, I get the following error:
Caused by: java.lang.IllegalStateException: Cannot map handler ‘MyController#0’ to URL path [/method/{key}]: There is already handler of type [class OtherController$$EnhancerByCGLIB$$54a955d] mapped.
at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:390)
at org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.registerHandler(AbstractUrlHandlerMapping.java:362)
at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:82)
at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58)
at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119)
at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:72)
at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:109)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:88)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:393)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1415)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:518)
… 25 more
Is it at all possible to define them in different controllers or should I look at some (horrible) workaround (like renaming one of the URLs)?
It should work fine since Spring 3.1 (if new mapping implementation is enabled – it’s enabled by default if use
<mvc:annotation-driven>or@EnableWebMvcand don’t declare anyHandlerMappings manually).In older versions of Spring you have to put these methods into the same controller.