Search code examples
springspring-bootjhipsterspring-websocket

Spring - Websockets - JSR-356


I implemented websockets into my application. I copied the configuration and dependencies from jHipster generated app, but I am getting the following errors:

java.lang.IllegalArgumentException: No 'javax.websocket.server.ServerContainer' ServletContext attribute. Are you running in a Servlet container that supports JSR-356?

and

org.apache.catalina.connector.ClientAbortException: java.io.IOException: An established connection was aborted by the software in your host machine

I believe these errors are the reason for the socket connection not being consistent and the therefore the client is not able to send and/or receive any messages. I searched for a solution but other post didn't help (ie. adding glassfish dependencies).

These are my ws dependencies:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-messaging</artifactId>
        </dependency>

Do I need to include some other dependencies or is the problem elsewhere?


Solution

  • I found a solution here. I added these 2 beans:

        @Bean
        public TomcatServletWebServerFactory tomcatContainerFactory() {
            TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();;
            factory.setTomcatContextCustomizers(Collections.singletonList(tomcatContextCustomizer()));
            return factory;
        }
    
        @Bean
        public TomcatContextCustomizer tomcatContextCustomizer() {
            return new TomcatContextCustomizer() {
                @Override
                public void customize(Context context) {
                    context.addServletContainerInitializer(new WsSci(), null);
                }
            };
        }