gpt4 book ai didi

java - Spring RestController中的继承

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:07 27 4
gpt4 key购买 nike

我有一个处理 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 {
....
}
}

我想做的是创建另一个 Controller 来处理版本 2 请求。同时我想继承已经在版本 1 Controller 中的实现。这是因为从 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。我也不确定像上面那样扩展 Controller 是否应该起作用。

我知道我可以在同一个 Controller 的 RequestMapping 中使用多个路径,如下所示:

@RestController
@RequestMapping(value = { "v1/someResource", "v1/someResource" })
public class Controller_V1 {

}

但以上内容不适用于我的场景。

这是扩展 Controller 时的堆栈跟踪:

 [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)

最佳答案

问题在堆栈跟踪的第一行很明显:

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]

这是因为您让 Spring 为您的 bean 命名。默认情况下,Spring 将使用驼峰命名法中当前类的接口(interface)或父类(super class)的名称作为 bean 名称。

所以,你有这样的东西(由 Spring 给出的名称):

@RestController("controller_V1") //<-- that's the name of the Spring bean
@RequestMapping("v1/someResource")
public class Controller_V1

@RestController("controller_V1") //<-- that's the name of the Spring bean
@RequestMapping("v2/someResource")
public class Controller_V2 extends Controller_V1

如您所见,您只需要重命名第二个 bean:

@RestController("controller_V2")
@RequestMapping("v2/someResource")
public class Controller_V2 extends Controller_V1

IMO 你应该为你的类使用更好的名字。

关于java - Spring RestController中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996398/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com