【问题标题】:Tomcat 8 and WebsocketTomcat 8 和 Websocket
【发布时间】:2015-04-05 14:05:06
【问题描述】:

我在将应用程序安装到 tomcat 8 时遇到了麻烦。我正在使用 websocket 和 spring 4,但我不想使用 spring 内部 STOMP 机制,所以我决定遵循this 教程并实现了我的 websocket例行公事我的方式。 我从几个星期以来就开始开发并始终使用码头(maven jetty 插件)对其进行测试,并且一切正常。但是现在我想将我的应用程序部署到我们在 java 8 和 CentOS 上运行 tomcat 8.0.15 的生产服务器上,但它不起作用。

这里是源代码:

@WebListener
public class MyApplication implements ServletContextListener {

private final static String SERVER_CONTAINER_ATTRIBUTE = "javax.websocket.server.ServerContainer";

@Override
public void contextInitialized(ServletContextEvent sce) {

    ServletContext container = sce.getServletContext();

    final ServerContainer serverContainer = (ServerContainer) container.getAttribute(SERVER_CONTAINER_ATTRIBUTE);
    try {
        serverContainer.addEndpoint(new MyEndpointConfig(MyEndpoint.class, "/wstest"));
    } catch (DeploymentException e) {
        e.printStackTrace();
    }
}
}

这是错误:

java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer
at my.package.contextInitialized(MyApplication.java:23)

第 23 行是我对 ServerContainer 进行强制转换的地方。 我猜“container.getAttribute(SERVER_CONTAINER_ATTRIBUTE)”返回null,因此转换失败,但是为什么??

Jetty 9.2.3 一切正常。我还使用安装的本地 tomcat 8(最新的 8.0.18)和 Windows 7 上的最新 JDK 8 和相同的行为对其进行了测试。

你有什么办法解决这个问题吗?

非常感谢!

【问题讨论】:

  • The following bug report 看起来很相关。
  • 是的,这很有帮助。我找到了问题:javax.websocketjavax.websocket-api1.0provided我没有在 pom.xml 中添加提供的范围

标签: java maven spring-mvc tomcat websocket


【解决方案1】:

Appleman1234 指向一个非常有用的错误报告。见 Iyad Elian 评论:

仅供参考,我发现为什么会发生异常 javax.websocket-api 之类的 javax.servlet-api 需要在运行时在 tomcat 中排除。 jetty 更喜欢应用程序类加载器而不是它自己的,所以这不是问题。

<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.0</version>
    <scope>provided</scope>
</dependency>

这正是它不在 tomcat 上而是在码头上工作的原因。我刚刚添加了

<scope>provided</scope>

现在它工作正常。感谢 Appleman1234!

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 2015-09-16
    • 2013-11-06
    • 1970-01-01
    • 2015-12-28
    • 2015-09-17
    • 2014-11-24
    • 1970-01-01
    • 2015-01-14
    相关资源
    最近更新 更多