Spring 启动 websocket 1.2.1.RELEASE - bean 'subProtocolWebSocketHandler' 中的 IllegalArgumentException:没有处理程序

Spring boot websocket 1.2.1.RELEASE - IllegalArgumentException in bean 'subProtocolWebSocketHandler' : No handlers

所以我希望将我的项目从 spring boot 1.1.9.RELEASE 升级到 1.2.1.RELEASE。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>

然而,在启动时,我获得了:

Exception in thread "Thread-0" org.springframework.context.ApplicationContextException: Failed to start bean 'subProtocolWebSocketHandler'; nested exception is java.lang.IllegalArgumentException: No handlers
        at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:176)
        at org.springframework.context.support.DefaultLifecycleProcessor.access0(DefaultLifecycleProcessor.java:51)
        at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:346)
        at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:149)
        at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:112)
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:770)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483)
        at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:95)
        at com.springagain.Application.run(Application.java:17)
Caused by: java.lang.IllegalArgumentException: No handlers
        at org.springframework.util.Assert.isTrue(Assert.java:65)
        at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.start(SubProtocolWebSocketHandler.java:234)
        at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173)
        ... 8 more

这是我的 websocket 配置的样子

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfiguration extends
        AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay("/queue/", "/topic/");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // TODO Auto-generated method stub

    }

}

切换回 1.1.9.RELEASE 只有 spring-boot-starter-websocket 依赖项(并将所有其他 spring 引导依赖项保持在 1.2.1.RELEASE spring core at 4.1.4),异常消失。

看起来像一个错误,但有人可以确认吗?

更新:更多上下文 - 这是来自后端服务器代码 - 没有 websocket 客户端连接到它。目的是通过 RabbitMQ 发布 'interesting' 事件,然后这些事件可供来自暴露 websocket 端点的前端服务器的客户端使用。我的前端服务器上的代码添加了支持 Socksjs 的端点:

public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/push").withSockJS();
}

从这个角度来看,要么我的理解存在根本性缺陷 :),要么 Spring 过于热心地检查是否始终应该有一个 websocket 端点。

问题的根源是您没有在 registerStompEndpoints 中配置任何端点。尝试使用 STOMP 但未配置任何 STOMP 端点的应用程序将无法正常工作。

当您使用 Spring Boot 1.1.9.RELEASE 时,您的类路径中会有一些 Spring Framework 4.0.x jar。 Spring Framework 4.0.x 的 WebSocket 支持不会注意到错误配置并允许您的应用启动,即使它无法运行。 Spring Framework 4.1 的 WebSocket 支持注意到此错误配置并抛出异常,从而提醒您注意该问题。