【问题标题】:Spring 4 MVC and Websockets - No suitable default RequestUpgradeStrategySpring 4 MVC 和 Websockets - 没有合适的默认 RequestUpgradeStrategy
【发布时间】:2014-03-10 17:46:06
【问题描述】:

我需要 Websockets 在我的应用程序中进行实时更新。所以我找到了这个例子并一步一步地做here。我浏览了教程,最后在启动应用程序时遇到了这个异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name

'org.springframework.web.socket.server.support.DefaultHandshakeHandler#0': bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:不能 实例化 bean 类 [org.springframework.web.socket.server.support.DefaultHandshakeHandler]: 构造函数抛出异常;嵌套异常是 java.lang.IllegalStateException:没有合适的默认值 找到 RequestUpgradeStrategy

我搜索了很多,但我没有找到解决方案。

希望有人能帮助我,在此先感谢。

最好的问候,帕特里克

【问题讨论】:

    标签: java spring hibernate spring-mvc websocket


    【解决方案1】:

    我通过添加以下 maven 依赖项设法解决了这个问题:

    <dependency>
       <groupId>org.apache.tomcat.embed</groupId>
       <artifactId>tomcat-embed-websocket</artifactId>
       <version>7.0.52</version>
    </dependency>
    

    正如 Craig Otis 所指出的,如果您仍然计划部署到 Tomcat,则应该使用 &lt;scope&gt;test&lt;/scope&gt; 以确保您不会在构建工件中包含依赖项。

    【讨论】:

    • 这对我有用,当看到 No suitable default RequestUpgradeStrategy found 作为我们 JUnit 测试的一部分时。请注意,如果您仍然计划部署到 Tomcat,则应使用 &lt;scope&gt;test&lt;/scope&gt; 以确保您不会在构建工件中包含依赖项。
    • 我的 spring mvc 项目现在出现这个错误。原因是:java.lang.IllegalStateException: A ServletContext is required to access the javax.websocket.server.ServerContainer instance
    【解决方案2】:

    我在使用嵌入式 Jetty 在 IDE 中运行 websockets 时遇到了这个问题,在将以下依赖项添加到 pom.xml 后修复了这个问题

                <dependency>
                    <groupId>org.eclipse.jetty.websocket</groupId>
                    <artifactId>websocket-client</artifactId>
                    <version>9.3.4.RC0</version>
                    <!--<scope>test</scope>-->
                </dependency>
                <dependency>
                    <groupId>org.eclipse.jetty.websocket</groupId>
                    <artifactId>websocket-server</artifactId>
                    <version>9.3.4.RC0</version>
                    <!--<scope>test</scope>-->
                </dependency>
                <dependency>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-client</artifactId>
                    <version>9.3.4.RC0</version>
                    <!--<scope>test</scope>-->
                </dependency>
    

    【讨论】:

      【解决方案3】:

      您使用的 Tomcat 版本太旧。

      升级tomcat。 http://tomcat.apache.org/download-70.cgi

      【讨论】:

      • 我正在使用 Tomcat7 并收到此错误。升级到 Tomcat8 为我解决了这个问题。谢谢
      【解决方案4】:

      我浪费了很多时间来寻找解决方案,我发现 spring websocket 只能在 Tomcat 7.0.47+、Jetty 9.1+、GlassFish 4.1+、WebLogic 12.1.3+ 和根据此处显示的 spring 文档,Undertow 1.0+(和 WildFly 8.0+) http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html,所以请尝试更新您的应用服务器

      【讨论】:

        【解决方案5】:

        我也遇到了这个问题,找了个链接看看有没有用https://github.com/rstoyanchev/spring-websocket-portfolio/issues/21

        【讨论】:

        • 这个链接救了我!显然,我的路径中只有 tomcat-embed-core、el 和 logging-juli 罐子!我也需要 tomcat-embed-websocket jar :D
        【解决方案6】:

        异常意味着 DefaultHandshakeHandler 找不到支持的服务器(例如 Tomcat 7 & 8、Jetty 9)。请参阅 javadoc here

        【讨论】:

          【解决方案7】:

          此外,

          如果你使用 gradle,你应该在 build.gradle 中添加以下依赖

          compile 'org.projectreactor:reactor-spring:1.0.1.RELEASE'

          【讨论】:

            猜你喜欢
            • 2015-01-28
            • 2015-04-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多