我创建了一个简单的Websocket应用程序并尝试针对tomcat版本7.0.53进行测试。当我构建我的应用程序时,我通过在pom.xml中使用以下Maven依赖项来获取tomcat-websocket-api jar文件:
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-websocket-api</artifactId>
<version>7.0.53</version>
</dependency>
这很有效,因为应用程序构建正常。当我将war文件部署到tomcat并尝试连接到我的应用程序定义的websocket端点时,它会失败,并且tomcat日志中会显示以下异常:
May 13, 2014 12:29:40 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [default] in context with path [/WebSocketRestApp] threw exception [Upgrade failed] with root cause
javax.servlet.ServletException: Upgrade failed
at org.apache.tomcat.websocket.server.UpgradeUtil.doUpgrade(UpgradeUtil.java:190)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
我发现如果我从pom.xml文件中删除对tomcat-websocket-api的依赖,如上所示,而是从tomcat安装中手动复制jar文件tomcat-websocket-api-7.0.53.jar ,并根据该jar文件构建我的战争,然后我可以毫无问题地连接到websocket。
两个jar文件(一个与tomcat捆绑在一起,另一个由maven下载)是相同的大小,但是它们必须有所不同。我更愿意从maven repo下载jar文件,但我不确定为什么这不起作用。任何建议表示赞赏。