1

I am trying to configure web sockets with spring using a Wildfly 10 server. As per this tutorial, I have the following files:

This is the web socket class:

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(); } } 

This is the web socket config file:

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(); } } 

This is my pom.xml:

 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>1.4.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>1.4.0.RELEASE</version> </dependency> 

According to the tutorial, this is all I have to do. But I get the following errors:

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 

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 /serverendpoint as I did in my serverendpoint() 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 others have included other files with, for instance, the @EnableWebSocket, but the tutorial says that I only need these two files.)?

Thank you so much!

1 Answer 1

1

Please go through https://github.com/spring-projects/spring-boot/issues/6166 and see if this solves your issue. There is a similar issue reported in SO at Spring Boot Websockets in Wildfly. Hope this helps.

Sign up to request clarification or add additional context in comments.

9 Comments

I will look into that, thank you so much. But aside from the config file, do I need any other files for my web socket to work on spring?
The tutorial you are following has a sample project available at github.com/rstoyanchev/spring-websocket-test. Please see if that can help you.
Now, I get a new error: Caused by: javax.websocket.DeploymentException: UT003023: Multiple endpoints with the same logical mapping PathTemplate{template=false, base='/serverendpoint', parts=[]} and PathTemplate{template=false, base='/serverendpoint', parts=[]}
Did you change something? Try maven clean package, un-deploy old build from jboss and restart the server.
No, though I added an undertow dependency to my pom.xml
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.