gpt4 book ai didi

Spring Websocket,连接时出现 404 错误。应用程序未使用 Spring MVC

转载 作者:行者123 更新时间:2023-11-28 23:25:57 27 4
gpt4 key购买 nike

我正在尝试开始使用 Spring 示例教程: https://spring.io/guides/gs/messaging-stomp-websocket/

但我需要它在我需要添加 WebSocket 支持的现有应用程序上工作。我的想法是,一旦我完成了基本工作,就从那里开始构建。

我与 URL 中示例的不同之处在于:

  1. 我没有使用 SpringBootApplication,而是使用 Tomcat (7.0.69)
  2. 这意味着我确实有一个 web.xml(包含在下面)
  3. 我跳过了 Application.java 的主要...我正在构建一个 WAR 并将其手动部署到 Tomcat 中。

当我启动 Tomcat 时,我阅读了以下似乎相关的内容:

03:06:52,908  INFO SimpleBrokerMessageHandler:157 - Starting...
03:06:52,908 INFO SimpleBrokerMessageHandler:260 - BrokerAvailabilityEvent[available=true, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
03:06:52,913 INFO SimpleBrokerMessageHandler:166 - Started.

问题如下:当我点击 index.html 上的连接时,当我试图通过映射 url“/hello”访问服务器时,我得到一个 404(不确定为什么信息在那里,我猜是协议(protocol)的一部分..) http://localhost:8080/hello/info

我正在谷歌搜索这个,甚至在 stackoverflow 中找到了一些答案。有些人已经修复了这个添加作为前缀的 DispatcherServlet 映射到 websocket url...

但是我的应用程序没有使用 Spring MVC,所以我没有配置 DispatcherServlet...而且我看起来只为 WebSockets 包含它有点矫枉过正。

我尝试将注释@EnableWebMvc 添加到 WebSocketConfig 类(在链接中是代码,我有相同的行)

任何建议将不胜感激!

WEB.XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>App</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

最佳答案

我放弃了避免为不使用 Spring MVC 的 APP 使用 MVC 调度程序的尝试。

这解决了它:

网络.xml

    <servlet>
<!-- Only used by websockets -->
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/websockets-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVCDispatcherServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

然后,在 index.html 中,我只在连接时添加了前缀/ws/(只有在那里,发送没有前缀的消息是可以的)。

关于Spring Websocket,连接时出现 404 错误。应用程序未使用 Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37043865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com