我有一个Spring RestController,它处理API调用的版本1。继承在Spring RestController
package rest.v1;
@RestController
@RequestMapping("v1/someResource")
public class Controller_V1 {
@RequestMapping(value = "/{resourceName}", method = RequestMethod.GET)
public Object retrieve() throws Exception {
....
}
}
我想要做的是创建另一个处理版本2请求的控制器。同时,我想继承版本1控制器中已有的实现。这是因为从v1到v2保持不变的实现将从v1继承。
因此,像这样:
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
}
}
否则它会导致ConflictingBeanDefinitionException。我也不确定扩展如上所述的控制器是否应该起作用。
我知道我可以使用多条路径在同一个控制器的RequestMapping这样的:
@RestController
@RequestMapping(value = { "v1/someResource", "v1/someResource" })
public class Controller_V1 {
}
但上面不会为我的方案工作。
下面是当控制器被扩展堆栈跟踪:
[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)
2015-10-07
SJha
+0
请告诉我们您的XML配置。 –