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



控制器代码:

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

WebSocketConfig:

@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配置的一部分加载,而不是作为根配置。

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

如果你把它放在根上下文中,一切都会正常工作,你可以使用SimpMessagingTemplate, broker relay,但不能在控制器中使用@MessageMapping。

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

在WebSocketConfig中尝试@ComponentScan,以便Spring可以找到控制器。

相关内容

  • 没有找到相关文章

最新更新