【发布时间】:2023-09-22 06:41:01
【问题描述】:
我正在尝试将使用 STOMP 的 WebSockets 支持添加到使用 XML 配置的 Spring MVC 应用程序中。到目前为止,一切都很顺利,我已经设法让 WebSockets 服务器监听,并且 stomp.js 可以连接到它并发送消息和接收响应。
我还没有设法开始工作的是支持服务器向客户端发送任意消息,这些消息不是对从客户端收到的消息的响应。这意味着到目前为止,这实际上只是一个更复杂的 REST 版本,并不太有用。
我的 XML 配置如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<websocket:message-broker>
<websocket:stomp-endpoint path="/api/websocket/stomp" allowed-origins="*">
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic,/queue" />
<websocket:message-converters>
<bean class="org.springframework.messaging.converter.MappingJackson2MessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</websocket:message-converters>
</websocket:message-broker>
<bean class="uk.co.grahamcox.webapp.DebugController">
<constructor-arg name="clock" ref="clock" />
<constructor-arg name="template" ref="brokerMessagingTemplate" />
</bean>
</beans>
(DebugController 是一个只有一个方法来返回服务器时间的类,可以作为 REST 和 WS 处理程序正常工作)
在启动时我得到:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'brokerMessagingTemplate' is defined
令人沮丧的是,IntelliJ 为我自动完成了“brokerMessagingTemplate”引用,我可以点击它进入 AbstractMessageBrokerConfiguration 中的 @Bean 定义。
我假设我在 XML 中遗漏了一些配置来完成这项工作,但我无法在文档中找到这是什么。
有什么建议吗?
【问题讨论】:
标签: java spring spring-mvc stomp spring-websocket