Java WebSocket 握手:意外响应代码:404 -

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30500998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:43:28  来源:igfitidea点击:

WebSocket handshake: Unexpected response code: 404 -

javajavascripttomcatwebsocket

提问by sam

I am learning about websockets and followed the instructions here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html

我正在学习 websockets 并按照此处的说明进行操作:http: //www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html

I' using tomcat v8.0.21 to deploy my webapp but get the following error

我使用 tomcat v8.0.21 部署我的 web 应用程序,但收到以下错误

My JavaScript Project looks fine, when I visit the webapp: my localhost and visit WebSocketJavaScriptProject

当我访问 webapp: my localhost 并访问 WebSocketJavaScriptProject 时,我的 JavaScript 项目看起来不错

Although in the console window get the error: WebSocket connection to 'ws://localhost:8080/WebsocketHome/actions' failed: Error during WebSocket handshake: Unexpected response code: 404

尽管在控制台窗口中出现错误:WebSocket connection to 'ws://localhost:8080/WebsocketHome/actions' failed: Error during WebSocket handshake: Unexpected response code: 404

My code lives here: https://github.com/darkcloudi/WebSocketProject

我的代码住在这里:https: //github.com/darkcloudi/WebSocketProject

Difference between my app and the example on Oracle is I have split it as two projects one service and the other webapp.

我的应用程序和 Oracle 上的示例之间的区别是我将它拆分为两个项目,一个服务和另一个 web 应用程序。

I cannot figure out why I would be getting a 404 as the service .war file is called WebsocketHome. Any ideas what would be causing this issue? Usually a 404 is not found so guessing it cannot finding the service , i'm sure i'm doing something stupid but cannot figure out where the problem is

我不明白为什么我会得到 404,因为服务 .war 文件被称为 WebsocketHome。任何想法会导致这个问题?通常找不到 404,所以猜测它找不到服务,我确定我在做一些愚蠢的事情,但无法弄清楚问题出在哪里

Thanks

谢谢

采纳答案by duffy356

The Error during the handshake happens, because Tomcat has its own api for websockets. Thus you might have added the JSR implementation or something similar as javax.websocket-api in your pom.xml there comes a conflict at runtime.

握手期间发生错误,因为 Tomcat 有自己的 websockets api。因此,您可能在 pom.xml 中添加了 JSR 实现或类似于 javax.websocket-api 的内容,但在运行时会发生冲突。

Try to not export your Websocket-library to your webserver, thus it uses its own implementation.

尽量不要将您的 Websocket 库导出到您的网络服务器,因此它使用自己的实现。

If you use maven, set the websocket dependency as provided:

如果您使用 maven,请按照提供的方式设置 websocket 依赖项:

<!-- Provided Websocket API, because tomcat has its own implementation -->
<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.1</version>
    <scope>provided</scope>
</dependency>

回答by mahem

I also had the same problem. Try removing the unwanted jars which contains websocket in your $JAVA_HOME/lib folder. and then add only what you are importing in serverendpoint file.

我也有同样的问题。尝试删除 $JAVA_HOME/lib 文件夹中包含 websocket 的不需要的 jar。然后仅添加您在 serverendpoint 文件中导入的内容。