微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

@ServerEndpoint 不适用于 Spring Boot v2.3.7

如何解决@ServerEndpoint 不适用于 Spring Boot v2.3.7

服务器正在运行,但我无法使用 chatendpoint.java 类。所以 spring 不使用 /user 路径。而且我无法连接 ws://localhost:8080/user 。当我尝试时出现错误

@ServerEndpoint(value = "/user")
public class ChatEndpoint {
    private Session session;
    private static final Set<ChatEndpoint> chatEndpoints = new copyOnWriteArraySet<>();
    private static HashMap<String,String> users = new HashMap<>();

    @Onopen
    public void Onopen (Session session) {
        System.out.println(" asdasd");
        System.out.println(session.toString()+" asdasd");
    }
      @OnMessage
    public void onMessage(Session session,Message message) throws 
    IOException,EncodeException {
       System.out.println("send message");

    }
}

@Configuration
@EnableWebSocket
public class WebSocketConfig  {

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

@SpringBootApplication
public class WebsocketdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebsocketdemoApplication.class,args);
    }

}

我使用的是 spring-boot-starter-websocket 2.3.7.RELEASE 和 spring-websocket 5.2.12.RELEASE

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>2.3.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
        <version>5.2.12.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
        <version>5.2.12.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.7.RELEASE</version>
    </dependency>

解决方法

我找到了解决方案。删除 @EnableWebSocket 因为我使用的是提供特定 websocket 句柄的 @serverendpoint 注释。第二个我有

    @OnMessage
public void onMessage(Session session,Message message) throws IOException,EncodeException {
    System.out.println("send message");
   
}

导致编译错误的消息消息参数我用字符串消息参数更改了它。并将@component 添加到 Chatendpoint 类。因为spring和tomcat可以理解为组件。

       @Component
    @ServerEndpoint(value = "/user")
    public class ChatEndpoint {

    @OnOpen
    public void OnOpen (Session session) {
        
    }
   
       @OnMessage
    public void onMessage(Session session,String message) throws IOException,EncodeException {
        System.out.println("send message");
       
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。