I'm new to Spring (and asking questions on stackoverflow).
I'd like to start an embedded (Tomcat) server via Spring Boot and register a JSR-356 WebSocket endpoint to it.
This is the main method:
@ComponentScan
@EnableAutoConfiguration
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}
This is how the configuration looks:
@Configuration
public class EndpointConfig {
@Bean
public EchoEndpoint echoEndpoint() {
return new EchoEndpoint();
}
@Bean
public ServerEndpointExporter endpointExporter() {
return new ServerEndpointExporter();
}
}
The EchoEndpoint
implementation is straight-forward:
@ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class)
public class EchoEndpoint {
@OnMessage
public void handleMessage(Session session, String message) throws IOException {
session.getBasicRemote().sendText("echo: " + message);
}
}
For the second part I've followed this blog post: https://spring.io/blog/2013/05/23/spring-framework-4-0-m1-websocket-support.
However, when I run 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
The exception is further caused by a NullPointerException
in ServerEndpointExporter
, because the getServletContext
method on the applicationContext
returns still null
at this point.
Can somebody with a better understanding of Spring help me out? Thanks!