2013-07-29 40 views
2

我想要實現的第一個嘗試連接到服務提供者時,如果沒有會話可用 - 創建一個和第二個,因爲我的客戶端不是瀏覽器,但HttpClient我想connectionStatus方法返回JSON而不是HTML?春季社會 - 是否有可能,如果是的話如何覆蓋從ConnectController方法?

我試圖直接從ConnectController延長,但我懷疑很多是發生在幕後所以不太清楚究竟是什麼錯誤是告訴我身後:

@Controller 
@RequestMapping("/connect") 
public class MyConnectController extends ConnectController { 

    @Inject 
    public MyConnectController(
      ConnectionFactoryLocator connectionFactoryLocator, 
      ConnectionRepository connectionRepository) { 
     super(connectionFactoryLocator, connectionRepository); 
     // TODO Auto-generated constructor stub 
    } 

    @RequestMapping(value = "/{providerId}", method = RequestMethod.GET) 
    public String get(@PathVariable String providerId, NativeWebRequest request) { 
     return "I'll return a json string here, OK?"; 
    } 

    @Override 
    @RequestMapping(value = "/{providerId}", method = RequestMethod.POST) 
    public RedirectView connect(@PathVariable String providerId, 
      NativeWebRequest request) { 
     System.out.println("I'll create a new session here, OK?"); 
     return super.connect(providerId, request); 
    } 
} 

和錯誤堆棧跟蹤:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'myConnectController' bean method 
public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(java.lang.String,org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model) 
to {[/connect/{providerId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'myConnectController' bean method 
public org.springframework.web.servlet.view.RedirectView com.buzz.social.connect.MyConnectController.get(java.lang.String,org.springframework.web.context.request.NativeWebRequest) mapped. 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
    at java.lang.Thread.run(Thread.java:680) 
Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'myConnectController' bean method 
public java.lang.String org.springframework.social.connect.web.ConnectController.connectionStatus(java.lang.String,org.springframework.web.context.request.NativeWebRequest,org.springframework.ui.Model) 
to {[/connect/{providerId}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'myConnectController' bean method 
public org.springframework.web.servlet.view.RedirectView com.buzz.social.connect.MyConnectController.get(java.lang.String,org.springframework.web.context.request.NativeWebRequest) mapped. 
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:171) 
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:145) 
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:107) 
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:87) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:163) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483) 
    ... 22 more 
+1

看起來像是多次加載控制器。這個'myConnectController'控制器從哪裏加載?我相信它是通過註釋,但你也從其他地方加載它 –

+0

它正在AppConfig中作爲一個bean被實例化。 –

回答

2

問題的原因在於ConnectController已有connectionStatus方法,其映射方法與您的get(...)方法完全相同:

@RequestMapping(value="/{providerId}", method=RequestMethod.GET) 
public String connectionStatus(@PathVariable String providerId, NativeWebRequest request, Model model) { 
    .... 
} 

將您的get(...)方法的映射更改爲不同的內容以防止發生衝突。

相關問題