32

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

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

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/2 And none of the proposed solutions seem to work for me. Here are the dependencies from my 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>

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

@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. :/

Chris Stier
  • 421
  • 1
  • 4
  • 5
  • I was also facing same problem while building the war using gradle. I was able to solve the problem by using `providedCompile` for `spring-boot-starter-tomcat` instead of `compile`. – Tariq M Nasim Dec 29 '16 at 05:22
  • the above issue can be resolved by excluding `spring-boot-starter-tomcat:jar` from the classpath [refer](https://github.com/spring-projects/spring-boot/issues/6166#issuecomment-225912741) – Prasanth Rajendran Dec 29 '22 at 09:02

6 Answers6

54

You need exclude 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>
jlgranda
  • 679
  • 5
  • 6
15

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

     <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.

Vikram Palakurthi
  • 2,406
  • 1
  • 27
  • 30
4

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

<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, jackson-databind, spring-boot-starter-web are redundant.

There is also spring-boot-starter-thymeleaf.

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

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Thanks so much for the reply. I changed my pom to eliminate the redundant deps and added tomcat-embed-websocket but am still getting the above exception. I guess the container is expecting the undertow web socket implementation? Why would my spring boot app try to use undertow when boot should control the entire container, no? – Chris Stier Sep 11 '14 at 19:25
  • Of course no. Spring just delegates to the target container implementation, similar to JTA. You should check the classpath of your app (WEB-INF/lib ?) for the Tomcat jars and see which deps should be excluded as well. – Artem Bilan Sep 11 '14 at 19:42
  • Art, thanks so much! Intellij wasn't syncing my libs. Noob mistake on my part. However I did have to add spring-messaging back in. I think I needed it directly for my MessageMapping annotation in my controller. But anyway, I'm good now. Thanks again! – Chris Stier Sep 11 '14 at 20:41
2

Use the following command to find all dependencies that includes 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.

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

<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

PPeter
  • 51
  • 1
  • 3
0

For me , spring-boot-starter-websocket and spring-boot-starter-web by default includes the spring-boot-starter-tomcat so exclude the tomcat, like below without version of spring boot:

<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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
doudou
  • 50
  • 9
0

Well, none of the solutions given above worked for me.

It was actually the version in the parent which was creating the problem.

Not WORKING:

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

WORKING:

     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
Yogesh Gandhi
  • 111
  • 1
  • 7