gpt4 book ai didi

Spring 4 WebSocket + sockJS :. 在跟踪中获取 "No matching method found"且未调用 @MessageMapping 处理程序

转载 作者:行者123 更新时间:2023-12-02 08:28:30 25 4
gpt4 key购买 nike

Controller 代码:

@Controller
public class SockController {

@MessageMapping(value="/chat")
public void chatReveived(Message message, Principal principal) {
...
LOGGER.debug("chatReveived message [{}]", message);
...
}
}

WebSocket配置:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue/", "/topic/");
config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/portfolio").withSockJS();
}
}

Javascript:

        var socket = new SockJS('/portfolio');
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
...
});
stompClient.send("/app/chat", {}, JSON.stringify(message))

通过这些代码,前端可以通过 WebSocket 连接到服务器并发送消息。但 @MessageMapping 处理程序方法 chatReveived() 不会被调用。

前端输出:

Opening Web Socket...
Web Socket Opened...
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

<<< CONNECTED
user-name:1
heart-beat:0,0
version:1.1

>>> SEND
destination:/app/chat
content-length:35

{"from":{"userId":1},"text":"ssss"}

服务器输出:

[21:19:26.551] TRACE org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator: TextMessage payload= SEND
desti.., byteCount=82, last=true], SockJsSession[id=mdibjok1, state=OPEN, sinceCreated=8504, sinceLastActive=8504]
-
[21:19:26.551] DEBUG org.springframework.messaging.simp.stomp.StompDecoder: Decoded [Payload byte[35]][Headers={stompCommand=SEND, nativeHeaders={content-length=[35], destination=[/app/chat]}, simpMessageType=MESSAGE, simpDestination=/app/chat, id=b7f01f0b-db3e-911d-60dc-c7275f8ef306, timestamp=1407201566551}]
-
[21:19:26.551] TRACE org.springframework.web.socket.messaging.StompSubProtocolHandler: Received message from client session=mdibjok1
-
[21:19:26.551] TRACE org.springframework.messaging.support.ExecutorSubscribableChannel: [clientInboundChannel] sending message id=0f482da7-fee0-d8f1-4b47-bd993eaee80d
-
[21:19:26.551] TRACE org.springframework.messaging.support.ChannelInterceptorChain: postSend (sent=true) message id 0f482da7-fee0-d8f1-4b47-bd993eaee80d
-
[21:19:26.552] DEBUG org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler: Handling message, lookupDestination=/chat
-
[21:19:26.553] DEBUG org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler: No matching method found
-
[21:19:26.553] TRACE org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler: Ignoring message to destination=/app/chat
-
[21:19:26.554] TRACE org.springframework.messaging.simp.user.DefaultUserDestinationResolver: Ignoring message to /app/chat, not a "user" destination

似乎无法找到处理程序方法。知道我哪里错了吗?

我的环境是:Tomcat 8.0.9、Spring 4.0.6 RELEASE、Spring security 3.2.4.RELEASE、JDK 7

最佳答案

WebSocketConfig 配置类应作为 Servlet 配置的一部分加载,而不是 Root 配置。

确保WebSocketConfig.classAbstractAnnotationConfigDispatcherServletInitializer.getRootConfigClasses() 的实现返回

如果你把它放在根上下文中,一切都会正常工作,你可以使用SimpMessagingTemplate,代理中继,但不能在 Controller 中使用@MessageMapping。

可以在 WebSocketAnnotationMethodMessageHandler.initControllerAdviceCache() 设置断点并检查上下文中加载了哪些 bean。如果没有@Controller在该方法中标记的 bean,@MessageMapping不会工作。

关于Spring 4 WebSocket + sockJS :. 在跟踪中获取 "No matching method found"且未调用 @MessageMapping 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129993/

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