SpringBoot项目使用WebSocket提示Error creating bean with name ‘serverEndpointExporter‘

问题描述:WebSocket在Controller中正常工作,但是在之后使用SpringBootTest进行单元测试的时候,突然提示WebSocket的相关错误。

错误提示:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [cn/edu/zzu/config/WebSocketConfiguration.class]: jakarta.websocket.server.ServerContainer not available
java.lang.IllegalStateException: Failed to load ApplicationContext for [WebMergedContextConfiguration@7686f701 testClass = cn.edu.zzu.ZzuServerApplicationTests, locations = [], classes = [cn.edu.zzu.ZzuServerApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@173ed316, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@27406a17, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3a7442c7, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@5032714f, org.springframework.boot.test.context.SpringBootTestAnnotation@e578dfe0], resourceBasePath = "src/main/webapp", contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]

	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:130)
	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:191)
	at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:130)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:260)
	at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:163)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:310)
	at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
	at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
	at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762)
	at java.base/java.util.Optional.orElseGet(Optional.java:364)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [cn/edu/zzu/config/WebSocketConfiguration.class]: jakarta.websocket.server.ServerContainer not available
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:962)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
	at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$3(SpringBootContextLoader.java:137)
	at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58)
	at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46)
	at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1463)
	at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.run(SpringBootContextLoader.java:553)
	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:137)
	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:108)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:225)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:152)
	... 17 more
Caused by: java.lang.IllegalStateException: jakarta.websocket.server.ServerContainer not available
	at org.springframework.util.Assert.state(Assert.java:76)
	at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterPropertiesSet(ServerEndpointExporter.java:107)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1835)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784)
	... 38 more

导致这个问题的原因:在测试环境中没有WebSocket的环境,只需要添加一个标识即可。

// 修改后的注解,再次运行就不会提示WebSocket的错误
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// @SpringBootTest  // 默认注解
@Slf4j
class ZzuServerApplicationTests {
    // 测试代码
}

相关推荐

  1. Springboot配置websocket,https使用 WebSocket 连接

    2024-07-09 20:22:04       31 阅读
  2. SpringBoot项目启动提示端口号占用

    2024-07-09 20:22:04       30 阅读
  3. SpringBoot中的WebSocket使用介绍

    2024-07-09 20:22:04       54 阅读
  4. SpringBoot使用Websocket控制评测机

    2024-07-09 20:22:04       38 阅读
  5. 使用springboot 配置一个websocket客户端

    2024-07-09 20:22:04       29 阅读
  6. springboot-WebSocket

    2024-07-09 20:22:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 20:22:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 20:22:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 20:22:04       58 阅读
  4. Python语言-面向对象

    2024-07-09 20:22:04       69 阅读

热门阅读

  1. LVS+keepalived群集

    2024-07-09 20:22:04       29 阅读
  2. qt udp通讯应用举例

    2024-07-09 20:22:04       29 阅读
  3. Vuetify3:v-data-table增加下拉筛选

    2024-07-09 20:22:04       18 阅读
  4. 通过升级tomcat完美解决服务器的tomcat漏洞

    2024-07-09 20:22:04       24 阅读
  5. unity 使用UnityWebRequest从服务器下载

    2024-07-09 20:22:04       20 阅读
  6. el-table 树状表格查询符合条件的数据

    2024-07-09 20:22:04       24 阅读
  7. for in和for of对比

    2024-07-09 20:22:04       23 阅读
  8. OpenHarmony4.x 系统模拟器环境

    2024-07-09 20:22:04       19 阅读
  9. PDF转Markdown的开源工具解析

    2024-07-09 20:22:04       29 阅读
  10. 力扣1878.矩阵中最大的三个菱形和

    2024-07-09 20:22:04       35 阅读
  11. 持续学习的艺术:SKlearn中模型的在线学习实践

    2024-07-09 20:22:04       31 阅读
  12. 代码随想录刷题day02-矩阵

    2024-07-09 20:22:04       29 阅读
  13. ListView

    ListView

    2024-07-09 20:22:04      31 阅读