Spring 4 WebSocket + sockJS:获取"找不到匹配的方法" in trace和@MessageMapping处理程序未被调用

时间:2014-08-05 01:36:18

标签: spring websocket stomp sockjs spring-4

控制器代码:

@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

2 个答案:

答案 0 :(得分:2)

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

确保从WebSocketConfig.class

的实施中返回AbstractAnnotationConfigDispatcherServletInitializer.getRootConfigClasses()

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

您可以在WebSocketAnnotationMethodMessageHandler.initControllerAdviceCache()中设置断点,并检查上下文中加载的beanss。如果该方法中没有@Controller标记的bean,则@MessageMapping将无效。

答案 1 :(得分:0)

在WebSocketConfig中尝试@ComponentScan,以便Spring可以找到Controller。