解决SpringBoot 1.5 使用 websocket,junit,tomcat 冲突异常

解决SpringBoot 1.5 使用 websocket,junit,tomcat 冲突异常

一、编译时,异常信息,可能原因是依赖包冲突,需要修改maven的作用范围
java.lang.IllegalStateException: Failed to load ApplicationContext

解决方法
1.修改 pom 文件

<!-- websocket编译时依赖tomcat容器,打成war包,外部运行环境不需要运行 -->
<!-- scope 包的范围配置成 provided  -->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>1.5.19.RELEASE</version>
        <scope>provided</scope>
</dependency>

<!-- 引入websocket 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
    <scope>compile</scope>
</dependency>

<!-- 添加junit依赖包 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

2.修改junit启动类

@RunWith(SpringRunner.class)
// 创建一个随机端口的真实的server容器,进行junit测试
@SpringBootTest(classes = 启动类入口.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// @SpringBootTest
public class TestApplicationTests

二、部署时,异常信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [com/jdrx/install/config/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available

原因分析:可能是 由于 ServerContainer 和tomcat版本有冲突,无法被实例化,所以无法启动,

解决方法:
升级 Tomcat 版本为 8.5.51

三、部署后访问 ws:// 404 错误
异常信息:
Error during WebSocket handshake: Unexpected response code: 404"

问题原因:可能是由于nginx的ws:// 协议支持没有配置

解决方案:
修改nginx配置:
server {
                location / {
                    # ws:// 协议支持
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade";
                }
}

建议升级到 Springboot 2.x 版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值