I am new to Spring (and asking questions about stackoverflow).
I would like to start the embedded (Tomcat) server through Spring Boot and register the WebSocket JSR-356 endpoint.
This is the main method:
@ComponentScan @EnableAutoConfiguration public class Server { public static void main(String[] args) { SpringApplication.run(Server.class, args); } }
This is what the configuration looks like:
@Configuration public class EndpointConfig { @Bean public EchoEndpoint echoEndpoint() { return new EchoEndpoint(); } @Bean public ServerEndpointExporter endpointExporter() { return new ServerEndpointExporter(); } }
EchoEndpoint implementation is straightforward:
@ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class) public class EchoEndpoint { @OnMessage public void handleMessage(Session session, String message) throws IOException { session.getBasicRemote().sendText("echo: " + message); } }
In the second part, I followed this blog post: https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support .
However, when I launch the application, I get:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointExporter' defined in class path resource [hello/EndpointConfig.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute
Also, the exception is thrown by a NullPointerException in ServerEndpointExporter because the getServletContext method in applicationContext returns still null at this point.
Can someone with a better understanding of Spring help me? Thanks!
qwtel source share