为什么 webflux 客户端给出 404?

Why does the webflux client give a 404?

我正在尝试让 webflux 客户端连接到远程 websocket。 https://www.websocket.org/echo.html 有一个示例 websocket。只需按 "connect",我就可以让我的浏览器在那里发出 wss 请求。在我的浏览器开发人员工具栏中,我可以看到对 wss://echo.websocket.org/?encoding=text

发出了成功的请求

这个url在https://stackify.com/reactive-spring-5/中也提到了(那里的代码不起作用,因为"input"没有定义)。

但是,当我尝试使用 webflux 从 spring boot 2.0.0 访问相同的 url 时,我得到一个 404:

@PostConstruct
public void run() throws URISyntaxException {
    WebClient webClient = WebClient.create();
    Flux<String> r =
            webClient.get().uri("wss://echo.websocket.org/?encoding=text")
                    .retrieve()
                    .bodyToFlux(String.class)
                    .log();

    r.subscribe(res -> System.out.println("ok: " + res), ex -> System.out.println(ex));
}

错误:

org.springframework.web.reactive.function.client.WebClientResponseException: ClientResponse has erroneous status code: 404 Not Found

我最好的猜测是它以某种方式不适用于以 "wss://" 开头的 url。我可以更改代码以便请求成功吗?

WebClient 不是 WebSocket 客户端,ReactorNettyWebSocketClient(在您链接的文章中提到)是 WebSocketClient。请改用这个。