美文网首页@IT·互联网
SpringBoot使用war包发布,javax.websock

SpringBoot使用war包发布,javax.websock

作者: 戴手套敲代码的小哥 | 来源:发表于2019-11-30 09:00 被阅读0次

    SpringBoot打包成war之后,运行项目后websocket会报错并且崩溃,这个问题困扰了我一整天,今天终于找到原因了

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [org/xx/config/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
    

    由于打包后项目不再依赖内置tomcat,导致了在springboot内置tomcat正常的代码到了外置容器就不能运行

    解决方案

    方法一,在配置类中把serverEndpointExporter方法注释掉,或者直接删掉

    @Configuration
    public class WebSocketConfig {
        /**
         *  注入ServerEndpointExporter,
         *  这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
         */
    //    @Bean
    //    public ServerEndpointExporter serverEndpointExporter() {
    //        return new ServerEndpointExporter();
    //    }
    }
    

    方法二,为了方便进行项目维护,建议使用@Profile注解,完美解决,省去了开发和发布时候注释代码的操作

    @Configuration
    public class WebSocketConfig {
        /**
         *  注入ServerEndpointExporter,
         *  这个bean会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint
         */
        @Profile({"dev", "test"})
        @Bean
        public ServerEndpointExporter serverEndpointExporter() {
            return new ServerEndpointExporter();
        }
    }
    

    @Profile注解的参数为字符数组,当项目环境Active profiles为dev或者test时,@bean serverEndpointExporter会正常装配,当Active profiles是其他比如prod的时候,serverEndpointExporter会被忽略不进行装配
    欢迎关注我的CSDN博客:https://blog.csdn.net/yin_ol

    相关文章

      网友评论

        本文标题:SpringBoot使用war包发布,javax.websock

        本文链接:https://www.haomeiwen.com/subject/dconwctx.html