title: Enabling WebSockets using spring
body: <p>I am trying to configure web sockets with spring using a Wildfly 10 server. As per <a href="https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support" rel="nofollow noreferrer">this</a> tutorial, I have the following files:</p>
  
  <p>This is the web socket class:</p>
  
  <pre><code>package com.myapp.spring.web.controller;
  
  import javax.websocket.OnClose;
  import javax.websocket.OnError;
  import javax.websocket.OnMessage;
  import javax.websocket.OnOpen;
  import javax.websocket.server.ServerEndpoint;
  
  import org.springframework.web.socket.server.standard.SpringConfigurator;
  
  
  @ServerEndpoint(value="/serverendpoint", configurator = SpringConfigurator.class)
  
  /**
   * This class creates web sockets, opens, and maintains connection with the client
   */
  public class serverendpoint {
  
  
      @OnOpen
      public void handleOpen () {
          System.out.println("JAVA: Client is now connected...");
      }
  
      @OnMessage
      public String handleMessage (String message) {
  
          if (message.equals("ping"))
              return "pong";
          else if (message.equals("close")) {
              handleClose();
              return null;
          }
          System.out.println("JAVA: Received from client: "+ message);
          if (message.contains("//")) {
              MyClass mc = new MyClass(message);
              return mc.someMethod();
          } else {
              System.out.println("Message From Web Socket Not Understood");
              return null;
          }
      }
  
      @OnClose
      public void handleClose() {
          System.out.println("JAVA: Client is now disconnected...");
      }
  
      @OnError
      public void handleError (Throwable t) {
          t.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This is the web socket config file:</p>
  
  <pre><code>package com.myapp.spring.security.config;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.socket.server.standard.ServerEndpointExporter;
  import com.myapp.spring.web.controller.serverendpoint;
  
  @Configuration
  public class EndpointConfig {
  
      @Bean
      public serverendpoint serverendpoint() {
          return new serverendpoint();
      }
  
      @Bean
      public ServerEndpointExporter endpointExporter() {
          return new ServerEndpointExporter();
      }
  
  }
  </code></pre>
  
  <p>This is my <code>pom.xml</code>:</p>
  
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt; 
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
              &lt;version&gt;1.4.0.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
              &lt;version&gt;1.4.0.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>According to the tutorial, this is all I have to do. But I get the following errors:</p>
  
  <pre><code>Failed to start service jboss.undertow.deployment.default-server.default-host./ROOT: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./ROOT: java.lang.RuntimeException: java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to io.undertow.websockets.jsr.ServerWebSocketContainer
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:85)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
      at org.jboss.threads.JBossThread.run(JBossThread.java:320)
  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:231)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
      at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
      ... 6 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:104)
      at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
      at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)
      ... 8 more
  </code></pre>
  
  <p>What is the fix to this problem? In addition, are there any other config files I need to add in order for my web socket to be mapped correctly at the endpoint <code>/serverendpoint</code> as I did in my <code>serverendpoint()</code> class (I am asking this because I am a bit unsure if I only need one config file or not. It doesn't seem right. I looked around and <a href="https://stackoverflow.com/questions/34246382/websockets-jsr-356-spring-integration-serverendpoint">others</a> have included other files with, for instance, the <code>@EnableWebSocket</code>, but the tutorial says that I only need these two files.)?</p>
  
  <p>Thank you so much!</p>