Wildfly 中的 Spring Boot Websockets

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25792121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:03:21  来源:igfitidea点击:

Spring Boot Websockets in Wildfly

springwebsocketspring-bootwildflyspring-websocket

提问by Chris Stier

Hi I need to deploy my Spring Boot app into Wildfly 8.1 and I'm getting the following exception:

嗨,我需要将我的 Spring Boot 应用程序部署到 Wildfly 8.1 中,但出现以下异常:

Caused by: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:219) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72) at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final] at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final] ... 3 more Caused by: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer at io.undertow.websockets.jsr.Bootstrap$WebSocketListener.contextInitialized(Bootstrap.java:69) at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173) at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:190) ... 7 more

引起:java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer 无法在 io.undertow.servlet.core.DeploymentManagerImpl 处强制转换为 io.undertow.websockets.jsr.ServerWebSocketContainer。 deploy(DeploymentManagerImpl.java:219) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87) at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.start(UndertowDeploymentService.start(UndertowDeploymentService.java:87))在 org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final] 在 org.jboss.msc.service.ServiceControllerImpl $StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final] ... 3 more 引起:java.lang。ClassCastException:无法将 org.apache.tomcat.websocket.server.WsServerContainer 转换为 io.undertow.websockets.jsr.Bootstrap$WebSocketListener.contextInitialized(Bootstrap.java:69) 处的 io.undertow.websockets.jsr.ServerWebSocketContainer。 undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173) at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:190) ... 7 更多DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:190) ... 7 更多DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:190) ... 7 更多

It seems that my setup for websockets and messaging might be the culprit? I was looking at this https://github.com/joshlong/boot-examples/issues/2And none of the proposed solutions seem to work for me. Here are the dependencies from my pom:

看来我对 websockets 和消息传递的设置可能是罪魁祸首?我在看这个https://github.com/joshlong/boot-examples/issues/2并且没有一个提议的解决方案对我有用。这是我的 pom 的依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.6.RELEASE</version>
</parent>

<dependencies>

    <!--Testing-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${apache.httpcomponents.version}</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.Hymanson.core</groupId>
        <artifactId>Hymanson-databind</artifactId>
    </dependency>

    <!--Database-->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901.jdbc4</version>
    </dependency>
</dependencies>

I tried using the direct spring-boot-starter-websocket as well but get the same thing. Here is my websocket config too:

我也尝试使用直接的 spring-boot-starter-websocket 但得到了同样的结果。这也是我的 websocket 配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer{

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    super.configureMessageBroker(registry);
    registry.enableSimpleBroker("/ssp");
    registry.setApplicationDestinationPrefixes("/inc");
}

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

Thanks for taking the time to read this. Any help would me most appreciated. Also, this is my first boot application. :/

感谢您抽时间阅读。任何帮助都会让我最感激。此外,这是我的第一个启动应用程序。:/

回答by jlgranda

You need exclude tomcat-embed-websocket

您需要排除 tomcat-embed-websocket

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-websocket</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

回答by Vikram Palakurthi

spring-boot-starter-web and spring-boot-starter-websocket by default includes the spring-boot-starter-tomcat so exclude the tomcat, like below

spring-boot-starter-web 和 spring-boot-starter-websocket 默认包含 spring-boot-starter-tomcat,因此排除 tomcat,如下所示

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring-boot.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

and include undertow, make sure the tomcat exists no where in the classpath.

并包括 undertow,确保 tomcat 在类路径中不存在。

回答by Artem Bilan

I'm not sure, but your POM around websockets should be like this:

我不确定,但是您围绕 websockets 的 POM 应该是这样的:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

spring-messaging, spring-websocket, Hymanson-databind, spring-boot-starter-webare redundant.

spring-messaging, spring-websocket, Hymanson-databind,spring-boot-starter-web是多余的。

There is also spring-boot-starter-thymeleaf.

还有spring-boot-starter-thymeleaf

However I think the root of your cause is tomcat-embed-websocket.

但是我认为你的原因的根源是tomcat-embed-websocket.

回答by PPeter

Use the following command to find all dependencies that includes spring-boot-starter-tomcat:

使用以下命令查找包含的所有依赖项spring-boot-starter-tomcat

mvn dependency:tree -Dincludes=org.springframework.boot:spring-boot-starter-tomcat

Exclude Tomcat starter from all your dependencies listed by this command.

从此命令列出的所有依赖项中排除 Tomcat starter。

After that, you might need to add javax.servlet-apias a provided dependency:

之后,您可能需要添加javax.servlet-api作为提供的依赖项:

<dependency>
    <artifactId>javax.servlet</artifactId>
    <groupId>javax.servlet-api</groupId>
    <scope>provided</scope>
</dependency>

Gradle users with this issue: check out the comments on this ticket: https://github.com/spring-projects/spring-boot/issues/6166

有此问题的 Gradle 用户:查看对这张票的评论:https: //github.com/spring-projects/spring-boot/issues/6166