【SpringBoot】三十八、SpringBoot引入Websocket后打包报错

一、背景

今天在之前的 SpringBoot 项目中 引入了 Websocket,因为之前都弄过,所以开发测试阶段都很顺利,到了打包上传的时候就报错了,报错信息如下:

XxxxxApplicationTests.contextLoads » IllegalState Failed to load ApplicationCon…

大概意思就是无法加载应用程序之类的提示语吧,问题出在测试类,XxxxxApplicationTests,测试类代码如下:

package com.asurplus;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AsurplusApplicationTests {

    @Test
    void contextLoads() {

    }

}

这是创建 SpringBoot 项目时,自动生成的测试类

二、原因在于

我们在引入 Websocket 的时候,需要对 Websocket 进行配置,配置类信息如下:

package com.asurplus.common.websocket;

import com.asurplus.websocket.service.WebsocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 *
 * @author lizhou
 */
@Configuration
public class WebsocketConfig extends HttpSessionHandshakeInterceptor {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

三、解决方法

只需要在 @SpringBootTest 注解中加入 webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,如下:

package com.asurplus;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class AsurplusApplicationTests {

    @Test
    void contextLoads() {

    }

}

再次,打包,打包成功!

如您在阅读中发现不足,欢迎留言!!!

已标记关键词 清除标记
请教各位大佬: springboot2.0使用WebSocket 添加@Component启动报错 使用springboot内置tomcat (Apache Tomcat/9.0.13) 下面是报错信息: ``` Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-01-23 16:35:16.201 ERROR 6888 --- [ main] o.s.boot.SpringApplication : Application run failed java.lang.IllegalStateException: Failed to register @ServerEndpoint class: class lmy.com.magic.controller.WebSockTest$$EnhancerBySpringCGLIB$$21e933ed at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:158) ~[spring-websocket-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoints(ServerEndpointExporter.java:133) ~[spring-websocket-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.web.socket.server.standard.ServerEndpointExporter.afterSingletonsInstantiated(ServerEndpointExporter.java:111) ~[spring-websocket-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:863) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at lmy.com.magic.DemoApplication.main(DemoApplication.java:12) [classes/:na] Caused by: javax.websocket.DeploymentException: Cannot deploy POJO class [lmy.com.magic.controller.WebSockTest$$EnhancerBySpringCGLIB$$21e933ed] as it is not annotated with @ServerEndpoint at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:202) ~[tomcat-embed-websocket-9.0.13.jar:9.0.13] at org.springframework.web.socket.server.standard.ServerEndpointExporter.registerEndpoint(ServerEndpointExporter.java:155) ~[spring-websocket-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 12 common frames omitted ```
©️2020 CSDN 皮肤主题: 终极编程指南 设计师:CSDN官方博客 返回首页