2017-08-10 45 views
0

我有一個通用的基礎控制器。以及擴展該通用控制器的控制器列表。我已經看到一些examples,其中可能擴展控制器。但是我得到Caused by: java.lang.IllegalStateException: Ambiguous mapping錯誤。彈出mvc錯誤,同時擴展通用控制器

基地控制器:

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(); 
    } 

} 

兒童控制器1:

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

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

兒童控制器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 

我使用的春天4.3.10.RELEASE。任何幫助如何解決?謝謝。

+0

你確定你發佈了一切嗎?由於它看起來像/ apis –

+0

@OgnjenMišić的雙映射感謝您的回覆。是的,我已經發布了一切 –

+1

@DeepakAgrawal嘗試「@RestController」RequestMapping(value =「/ clusters」) –

回答

2

你可以嘗試:

@RestController 
@RequestMapping("/apis") 

@RestController 
@RequestMapping("/cluster") 
1

你有兩個孩子的控制器這樣的映射:

@RestController 
    RequestMapping (value= "/clusters") 
0

傳遞給@RestController的價值,定義Spring應用程序上下文中該bean的邏輯名稱,默認爲camelCase ve該類名稱的rsion。 @RequestMapping也可用於指定類的URL映射。