Wildfly中的Spring Boot Websockets

时间:2014-09-11 15:59:41

标签: spring websocket spring-boot wildfly spring-websocket

您好我需要将我的Spring Boot应用程序部署到Wildfly 8.1中,我遇到以下异常:

  

引起:java.lang.RuntimeException:java.lang.ClassCastException:org.apache.tomcat.websocket.server.WsServerContainer无法强制转换为io.undertow.websockets.jsr.ServerWebSocketContainer       在io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:219)       at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)       在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个   引起:java.lang.ClassCastException:org.apache.tomcat.websocket.server.WsServerContainer无法强制转换为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个

似乎我的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.jackson.core</groupId>
        <artifactId>jackson-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>

我也试过使用直接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");
}
}

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

6 个答案:

答案 0 :(得分:36)

您需要排除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>

答案 1 :(得分:7)

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>

并包含下载,确保tomcat不存在于类路径中的任何位置。

答案 2 :(得分:3)

我不确定,但是围绕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-messagingspring-websocketjackson-databindspring-boot-starter-web是多余的。

还有spring-boot-starter-thymeleaf

但是我认为你的事业的根源是tomcat-embed-websocket

答案 3 :(得分:2)

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

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

从此命令列出的所有依赖项中排除Tomcat启动程序。

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

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

让用户遇到此问题:请查看此故障单上的评论:https://github.com/spring-projects/spring-boot/issues/6166

答案 4 :(得分:0)

对我来说,默认情况下,spring-boot-starter-websocket和spring-boot-starter-web包括spring-boot-starter-tomcat,因此排除了tomcat,如下所示,其中没有使用spring boot版本:

information_schema.tables

答案 5 :(得分:0)

好吧,上面给出的解决方案都不适合我。

实际上是父版本中的版本造成了问题。

不工作:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

工作:

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>