Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在spring中运行javax.websocket端点?_Java_Spring_Websocket - Fatal编程技术网

在spring中运行javax.websocket端点?

在spring中运行javax.websocket端点?,java,spring,websocket,Java,Spring,Websocket,我已经按照本教程实现了websocket服务器: 现在如何运行此应用程序?我正在使用Spring,我的主要功能如下所示: import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void mai

我已经按照本教程实现了websocket服务器:

现在如何运行此应用程序?我正在使用Spring,我的主要功能如下所示:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
import java.io.IOException;

import javax.websocket.*;
import javax.websocket.server.*;

@ServerEndpoint(
        value = "/chat/{username}",
        decoders=MessageDecoder.class,
        encoders = MessageEncoder.class
)
public class Controller {
    // ...
}
我的websocket端点如下所示:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
import java.io.IOException;

import javax.websocket.*;
import javax.websocket.server.*;

@ServerEndpoint(
        value = "/chat/{username}",
        decoders=MessageDecoder.class,
        encoders = MessageEncoder.class
)
public class Controller {
    // ...
}
现在,当我运行此命令并尝试像这样连接到websocket端点时:
wsta ws://localhost:8080/chat/aa-I
我得到以下错误:

WebSocket升级请求

---

主机:本地主机:8080

连接:升级

升级:websocket

Sec WebSocket版本:13

Sec WebSocket密钥:

来源:

WebSocket升级响应

---

404找不到

更改:原始访问控制请求方法访问控制请求标头

内容类型:application/json

传输编码:分块

日期:2020年5月5日星期二12:12:52 GMT

WebSocket错误:WebSocket响应错误


这里的答案()表示您通过
ws://localhost:8080/context/chat/aa
访问WebCoket端点。什么是上下文,如何让服务器运行并接受传入的连接?

以便运行OP中提到的Baeldung教程

  • 我必须为
    springbootstarterwebsocket
    添加一个maven依赖项到
    pom.xml

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    
  • 此外,端点控制器必须是弹簧组件:

    @ServerEndpoint(
            value = "/chat/{username}",
            decoders=MessageDecoder.class,
            encoders = MessageEncoder.class
    )
    @Component
    public class Controller {
     ...
    


  • 最终,可以在
    ws://localhost:8080/chat/torvalds

    下访问websocket端点,非常感谢,这就是我所缺少的!