栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ServerEndpointExporter是什么 tomcat部署WebSocket服务,初始化ServerEndPointExporter失败问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ServerEndpointExporter是什么 tomcat部署WebSocket服务,初始化ServerEndPointExporter失败问题

原代码如下

@ServerEndPoint("push")
@Component
public class MyWebSocket{
    @OnOpen
    public void onOpen(Session session) {
        //收到客户端连接
    }

    @OnMessage
    public void onMessage(String message){
        //收到客户端发送的消息
    }
}       

@Configuration
public class WebSocketConfiguration{
     @Bean
    public ServerEndPointExporter serverEndPointExporter(){
        return new ServerEndPointExporter();
    }
}

上述是本地项目启动测试代码,调试都没有任何问题,但是打包后部署在tomcat中启动,就会报ServerEndPointExporter类加载失败问题

解决方案

1、直接删除serverEndPointExporter()方法上的bean注解,然后打包部署,测试websocket功能都能正常调用(本地测试就会有问题,本地需要将bean注解打开)

2、通过@profile注解,解决bean注解忘记注释的问题

@Configuration
public class WebSocketConfiguration{
    @Profile("test")
 	@Bean
    public ServerEndPointExporter serverEndPointExporter{
    	return new ServerEndPointExporter();
    }
}

//需要在application.properties中指定 spring.profiles.active = test
分析原因:

简单来说:就是ServerEndPointExporter,在springboot内置容器(嵌入式容器)中运行时,必须上下文提供ServerEndpointExporter

但是在tomcat容器中运行时,扫描工作会交给容器处理,不需要bean注入。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1008873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号