gpt4 book ai didi

java - 扩展通用 Controller 时 Spring mvc 错误

转载 作者:行者123 更新时间:2023-12-01 20:16:50 26 4
gpt4 key购买 nike

我有一个通用的基本 Controller 。以及扩展该通用 Controller 的 Controller 列表。我见过一些examples ,可以扩展 Controller 。但我收到了 Caused by: java.lang.IllegalStateException: Ambigously mapping 错误。

基本 Controller :

public abstract class BaseController<O extends BaseDomain, OT extends BaseDto> {

private AbstractService<O, OT> service;

public BaseController(AbstractService<O, OT> service) {
this.service = service;
}

@RequestMapping(value="/", method = RequestMethod.GET)
public ResponseObject list() {
return somework();
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseObject get(@PathParam("id") String id) {
return somework();
}

@RequestMapping(value="/", method = RequestMethod.POST)
public ResponseObject create(@Valid @RequestBody OT input, Errors errors) {
return somework();
}

@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
public ResponseObject update(@PathParam("id") String id, @Valid @RequestBody OT input, Errors errors) {
return somework();
}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public ResponseObject delete(@PathParam("id") String id) {
return somework();
}

}

子 Controller 1:

@RestController("/apis")
public class ApiController extends BaseController<Api, ApiDto>{

public ApiController(@Autowired ApiService apiService) {
super(apiService);
}
}

子 Controller 2:

@RestController("/clusters")
public class ClusterController extends BaseController<Cluster, ClusterDto>{

public ClusterController(@Autowired ClusterService clusterService) {
super(clusterService);
}
}

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/clusters' method 
public com.casa.authorizebase.dto.ResponseObject com.casa.authorize.controller.BaseController.get(java.lang.String)
to {[/{id}],methods=[GET]}: There is already '/apis' bean method
public com.casa.authorizebase.dto.ResponseObject com.casa.authorize.controller.BaseController.get(java.lang.String) mapped.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4633)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5095)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1425)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1415)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:941)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:657)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/clusters' method
public com.casa.authorizebase.dto.ResponseObject com.casa.authorize.controller.BaseController.get(java.lang.String)
to {[/{id}],methods=[GET]}: There is already '/apis' bean method
public com.casa.authorizebase.dto.ResponseObject com.casa.authorize.controller.BaseController.get(java.lang.String) mapped.
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 42 more

我正在使用 spring 4.3.10.RELEASE。有帮助如何修复吗?谢谢。

最佳答案

您可以尝试:

@RestController
@RequestMapping("/apis")

@RestController
@RequestMapping("/cluster")

关于java - 扩展通用 Controller 时 Spring mvc 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45606158/

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