title: Inheritance in Spring RestController
body: <p>I have a Spring RestController which handles version 1 of API calls.</p>
  
  <pre><code>package rest.v1;
  
  @RestController
  @RequestMapping("v1/someResource")
  public class Controller_V1 {
  
      @RequestMapping(value = "/{resourceName}", method = RequestMethod.GET)
      public Object retrieve() throws Exception {
          ....
      }
  }
  </code></pre>
  
  <p>What I want to do is create another controller which handles version 2 requests. At the same time I want to inherit the implementation which are already in version 1 controller. This is because the implementations which are unchanged from v1 to v2 will just be inherited as it is from v1.</p>
  
  <p>So something like this:</p>
  
  <pre><code>package rest.v2;
  
  @RestController("controllerV2")
  @RequestMapping("v2/someResource")
  public class Controller_v2 extends Controller_v1 {
  
      @RequestMapping(value = "/{resourceName}", method = RequestMethod.GET)
      public Object retrieve() throws Exception {
          //implementation overridden from v1
      }
  }
  </code></pre>
  
  <p>Doing it results in ConflictingBeanDefinitionException. I am also not sure if extending the controllers like above is supposed to work.</p>
  
  <p>I am aware that I can use multiple paths in RequestMapping of the same controller like this:</p>
  
  <pre><code>@RestController
  @RequestMapping(value = { "v1/someResource", "v1/someResource" })
  public class Controller_V1 {
  
  }
  </code></pre>
  
  <p>But the above is not going to work for my scenario. </p>
  
  <p>Here is the stack trace when the controllers are extended:</p>
  
  <pre><code> [java] org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [rest-context.xml]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'controller_V1' for bean class [rest.v2.Controller_V2] conflicts with existing, non-compatible bean definition of same name and class [rest.v1.Controller_V1]
       [java]     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:413)
       [java]     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
       [java]     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
       [java]     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
       [java]     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
       [java]     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
       [java]     at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
       [java]     at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
       [java]     at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
       [java]     at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
       [java]     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
       [java]     at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
       [java]     at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
       [java]     at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
       [java]     at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
       [java]     at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
       [java]     at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
       [java]     at javax.servlet.GenericServlet.init(GenericServlet.java:158)
  </code></pre>