如何修复缺少 WebSocket ServerContainer 以启用带有 CometD、Jetty 和 Spring 的 WebSockets?

How can I fix Missing WebSocket ServerContainer enabling WebSockets with CometD, Jetty and Spring?

我在尝试在 CometD 上启用 WebSockets 时遇到问题。我的配置如下:
- CometD 3.0.5
- 码头 9.0.6,
- SpringMVC 3.2.4


web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

    <servlet>
        <servlet-name>cometd</servlet-name>
            <servlet-class>org.cometd.server.CometDServlet</servlet-class>
            <async-supported>true</async-supported>
        </servlet>

        <servlet-mapping>
            <servlet-name>cometd</servlet-name>
            <url-pattern>/cometd/*</url-pattern>
        </servlet-mapping>
    </servlet>
    </web-app>

applicationContext.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  <bean id="bayeuxServer" class="org.cometd.server.BayeuxServerImpl" init-method="start" destroy-method="stop">
        <property name="options">
            <map>
                <entry key="javax.servlet.ServletContext" value-ref="servletContext" />
                <entry key="ws.cometdURLMapping" value="/cometd/*" />
                <entry key="logLevel" value="0" />
                <entry key="timeout" value="15000" />
            </map>
        </property>
        <property name="transports">
            <list>
                <bean id="websocketTransport" class="org.cometd.websocket.server.WebSocketTransport">
                    <constructor-arg ref="bayeuxServer" />
                </bean>
            </list>
        </property>
    </bean>
</beans>

我遇到的异常如下:

java.lang.IllegalArgumentException: Missing WebSocket ServerContainer
    at org.cometd.websocket.server.WebSocketTransport.init(WebSocketTransport.java:81)
    at org.cometd.server.BayeuxServerImpl.initializeServerTransports(BayeuxServerImpl.java:260)
    at org.cometd.server.BayeuxServerImpl.doStart(BayeuxServerImpl.java:122)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1608)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1549)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory.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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory.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:628)
    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 com.app.gui.web.appContextLoaderListener.contextInitialized(appContextLoaderListener.java:25)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:788)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:434)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:780)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:284)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1322)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:732)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:490)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
    at org.eclipse.jetty.server.Server.start(Server.java:342)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:100)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:60)
    at org.eclipse.jetty.server.Server.doStart(Server.java:290)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
    at com.app.jetty.AppJettyServer.main(appJettyServer.java:189)

将您的 Jetty 版本升级到 9。2.x 系列。

9.0.x 不支持您的配置试图启用的 JSR356。

截至 2015 年 10 月,最新稳定的 Jetty 9.2.x 系列版本是 9.2.13.v20150730

您使用的库版本严重不匹配。 坚持使用 CometD 3.0.6、Jetty 9.2.13 和 Spring 4.2.2.

设置骨架 CometD Web 应用程序的推荐方法是遵循 primer instructions。 如果您使用 Maven,这可以归结为一行来创建一个使用 Jetty Maven 插件开箱即用的骨架 CometD Web 应用程序:

$ mvn archetype:generate -DarchetypeCatalog=http://cometd.org

否则,您可以使用生成的 war 文件并将其部署到 these instructions 之后的 Jetty。