org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointExporter' defined in class sample.Application: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute

Spring JIRA | Andy Wilkinson | 2 years ago
  1. 0

    This Boot app illustrates the problem: {code} package sample; import java.io.IOException; import javax.servlet.ServletContext; import javax.websocket.OnMessage; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.ServletContextAware; import org.springframework.web.socket.server.standard.ServerEndpointExporter; import org.springframework.web.socket.server.standard.SpringConfigurator; @ComponentScan @EnableAutoConfiguration @Configuration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public EchoEndpoint echoEndpoint() { return new EchoEndpoint(); } @Bean public ServerEndpointExporter endpointExporter() { return new ServerEndpointExporter(); } @ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class) private static class EchoEndpoint { @OnMessage public void handleMessage(Session session, String message) throws IOException { session.getBasicRemote().sendText("echo: " + message); } } } {code} Running it fails with a {{NullPointerException}}: {noformat} Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointExporter' defined in class sample.Application: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:232) at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:618) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) at sample.Application.main(Application.java:26) Caused by: java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute at org.springframework.web.socket.server.standard.ServerEndpointExporter.getServerContainer(ServerEndpointExporter.java:113) at org.springframework.web.socket.server.standard.ServerEndpointExporter.setApplicationContext(ServerEndpointExporter.java:86) at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119) at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ... 14 more Caused by: java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.web.socket.server.standard.ServerEndpointExporter.getServerContainer(ServerEndpointExporter.java:110) ... 20 more {noformat} {{ServerEndpointExporter}} assumes that {{WebApplicationContext.getServletContext()}} will return a non-null value when it's called from within {{setApplicationContext(context)}}. This assumption doesn't hold true in a Boot application as the embedded Tomcat server hasn't been started yet. A work around is to replace the {{ServerEndpointExporter}} bean with the following: {code} @Bean public ServletContextAware endpointExporterInitializer(final ApplicationContext applicationContext) { return new ServletContextAware() { @Override public void setServletContext(ServletContext servletContext) { ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter(); serverEndpointExporter.setApplicationContext(applicationContext); try { serverEndpointExporter.afterPropertiesSet(); } catch (Exception e) { throw new RuntimeException(e); } } }; } {code} This defers {{ServerEndpointExporter}}'s processing until a time when the {{ServletContext}} is available

    Spring JIRA | 2 years ago | Andy Wilkinson
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointExporter' defined in class sample.Application: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute
  2. 0

    This Boot app illustrates the problem: {code} package sample; import java.io.IOException; import javax.servlet.ServletContext; import javax.websocket.OnMessage; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.ServletContextAware; import org.springframework.web.socket.server.standard.ServerEndpointExporter; import org.springframework.web.socket.server.standard.SpringConfigurator; @ComponentScan @EnableAutoConfiguration @Configuration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public EchoEndpoint echoEndpoint() { return new EchoEndpoint(); } @Bean public ServerEndpointExporter endpointExporter() { return new ServerEndpointExporter(); } @ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class) private static class EchoEndpoint { @OnMessage public void handleMessage(Session session, String message) throws IOException { session.getBasicRemote().sendText("echo: " + message); } } } {code} Running it fails with a {{NullPointerException}}: {noformat} Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointExporter' defined in class sample.Application: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:232) at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:618) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) at sample.Application.main(Application.java:26) Caused by: java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute at org.springframework.web.socket.server.standard.ServerEndpointExporter.getServerContainer(ServerEndpointExporter.java:113) at org.springframework.web.socket.server.standard.ServerEndpointExporter.setApplicationContext(ServerEndpointExporter.java:86) at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119) at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ... 14 more Caused by: java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.springframework.web.socket.server.standard.ServerEndpointExporter.getServerContainer(ServerEndpointExporter.java:110) ... 20 more {noformat} {{ServerEndpointExporter}} assumes that {{WebApplicationContext.getServletContext()}} will return a non-null value when it's called from within {{setApplicationContext(context)}}. This assumption doesn't hold true in a Boot application as the embedded Tomcat server hasn't been started yet. A work around is to replace the {{ServerEndpointExporter}} bean with the following: {code} @Bean public ServletContextAware endpointExporterInitializer(final ApplicationContext applicationContext) { return new ServletContextAware() { @Override public void setServletContext(ServletContext servletContext) { ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter(); serverEndpointExporter.setApplicationContext(applicationContext); try { serverEndpointExporter.afterPropertiesSet(); } catch (Exception e) { throw new RuntimeException(e); } } }; } {code} This defers {{ServerEndpointExporter}}'s processing until a time when the {{ServletContext}} is available

    Spring JIRA | 2 years ago | Andy Wilkinson
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpointExporter' defined in class sample.Application: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Failed to get javax.websocket.server.ServerContainer via ServletContext attribute
  3. Speed up your debug routine!

    Automated exception search integrated into your IDE

  1. tyson925 1 times, last 1 month ago
  2. ajinkya_w 2 times, last 2 months ago
  3. Ajeet 8 times, last 3 months ago
  4. qavid 2 times, last 6 months ago
  5. Nikolay Rybak 11 times, last 6 months ago
2 more registered users
11 unregistered visitors
Not finding the right solution?
Take a tour to get the most out of Samebug.

Tired of useless tips?

Automated exception search integrated into your IDE

Root Cause Analysis

  1. java.lang.NullPointerException

    No message provided

    at sun.reflect.NativeMethodAccessorImpl.invoke0()
  2. Java RT
    Method.invoke
    1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    4. java.lang.reflect.Method.invoke(Method.java:483)
    4 frames
  3. Spring WebSocket
    ServerEndpointExporter.setApplicationContext
    1. org.springframework.web.socket.server.standard.ServerEndpointExporter.getServerContainer(ServerEndpointExporter.java:110)
    2. org.springframework.web.socket.server.standard.ServerEndpointExporter.setApplicationContext(ServerEndpointExporter.java:86)
    2 frames
  4. Spring Context
    ApplicationContextAwareProcessor.postProcessBeforeInitialization
    1. org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)
    2. org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)
    2 frames
  5. Spring Beans
    AbstractBeanFactory.getBean
    1. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
    2. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1545)
    3. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    4. org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    5. org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    6. org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    7. org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    8. org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    8 frames
  6. Spring Context
    AbstractApplicationContext.refresh
    1. org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:232)
    2. org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:618)
    3. org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
    3 frames
  7. Spring Boot
    SpringApplication.run
    1. org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
    2. org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    3. org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    4. org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    5. org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    5 frames
  8. Java EE Connector Architecture API Specification
    Application.main
    1. sample.Application.main(Application.java:26)
    1 frame