我正在尝试在 Tomcat 8 上设置“Echo”websocket 示例。我相信我错过了告诉 tomcat 有关 websocket 类的步骤。
这是我所做的:
- 在 Eclipse 中创建了一个新的动态 Web 项目,添加了 Maven,并链接了 tomcat 8 运行时
- 将 EchoAnotation.java、EchoEndpoint.java、ExamplesConfig.java 和 echo.xhtml 复制到项目中。
- 启动应用。
当我启动应用程序时,我可以导航到 echo.xhtml 文件,但无论我输入什么 url,我似乎都不知道如何访问 Web 套接字 - 我总是得到 404。放置断点在构造函数中,看起来 websocket 类没有被加载。
通常情况下,您会在 web.xml 或其他一些框架配置中设置类配置,但我在 Tomcat8 SVN 上的示例 web.xml(或任何其他 xml)中找不到匹配的配置。
对如何完成此操作有任何见解吗?
谢谢!
源代码
EchoAnotation.java、EchoEndpoint.java、ExamplesConfig.java 位置:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/ http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/classes/websocket/echo
Echo.xhtml 位置:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/websocket/
web.xml 位置示例:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/examples/WEB-INF/
我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
最佳答案
想通了。最新版本的基于 war 的 Web 应用程序会自动进行组件扫描。我的不是,因为我已经包含了这个:
如果包含 tomcat,请勿包含!
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
</dependency>
Websocket 端点引用:
https://tyrus.java.net/documentation/1.3.3/index/deployment.html
关于java - Tomcat 8 示例 : Missing Configuration Step,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20723818/