1、問題描述
在 SpringBoot 項(xiàng)目中集成了 WebSocket,當(dāng)進(jìn)行 SpringBoot 單元測試時(shí)報(bào)錯(cuò):
javax.websocket.server.ServerContainer not available
報(bào)錯(cuò)的方法是:
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
2、產(chǎn)生原因
報(bào)錯(cuò)的方法的主要作用是:用于掃描帶有 @ServerEndpoint 的注解成為 websocket
,該方法是 服務(wù)器端點(diǎn)出口,當(dāng)進(jìn)行 SpringBoot 單元測試時(shí),并沒有啟動(dòng)服務(wù)器,所以當(dāng)加載到這個(gè)bean時(shí)會(huì)報(bào)錯(cuò)。文章來源:http://www.zghlxwxcb.cn/news/detail-603980.html
3、解決方法
為 SpringBoot 單元測試提供一個(gè)測試環(huán)境,在注解中添加webEnvironment,給wevsocket提供測試環(huán)境:文章來源地址http://www.zghlxwxcb.cn/news/detail-603980.html
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
到了這里,關(guān)于解決:javax.websocket.server.ServerContainer not available 報(bào)錯(cuò)問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!