Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud route to webscoket service causes "ResponseFacade cannot be cast to class HttpServerResponse"

I have this section in application.yml

  cloud:
    gateway:
      routes:

        - id: websocket
          uri: ws://localhost:53110
          predicates:
            - Path=/ws/**

        - id: websocket_sockjs_route
          uri: http://localhost:53110
          predicates:
            - Path=/ws/**

WebSocket configuration is pretty basic

 @Override
  public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/some_topic");
    registry.setApplicationDestinationPrefixes("/app");
  }

  @Override
  public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
  }

Once the browser starts. it attempts to establish web-socket connection to

  ws://localhost:53100/ws

First call goes well with Status Code "101 Switching Protocols" but the second call reports "Error during WebSocket handshake: Unexpected response code: 500" and from there on Chrome keeps reporting it every 10+ seconds.

In the application log I see following lines every time browser reports a 500. Any help, pointers will be greatly appreciated.

[AbstractErrorWebExceptionHandler]: [28b2eda4] 500 Server Error for HTTP GET "/ws"

java.lang.ClassCastException: class org.apache.catalina.connector.ResponseFacade cannot be cast to class reactor.netty.http.server.HttpServerResponse (org.apache.catalina.connector.ResponseFacade and reactor.netty.http.server.HttpServerResponse are in unnamed module of loader 'app') at org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy.upgrade(ReactorNettyRequestUpgradeStrategy.java:163) ~[spring-webflux-5.3.21.jar:5.3.21] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s): *__checkpoint ? org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain] *__checkpoint ? org.springframework.security.web.server.authorization.AuthorizationWebFilter [DefaultWebFilterChain]

I was expecting the browser to establish the connection to the endpoint.

Tried using Chrome and Edge. Chrome DevTools does not report specific error status but only

WebSocket connection to 'ws://localhost:53100/ws' failed:   client.js: 281 
like image 518
YazarBaby Avatar asked Sep 17 '26 21:09

YazarBaby


1 Answers

I also encountered a similar problem:

Gateway The default usage is based on Netty Container of RequestUpgradeStrategy and WebSocketClient Result in an error , Statement Tomcat The container corresponds to Bean To cover it can solve this problem

Here is my solution:

add follow beans to container

@Bean
@Primary
WebSocketClient tomcatWebSocketClient() {
    return new TomcatWebSocketClient();
}
@Bean
@Primary
public RequestUpgradeStrategy requestUpgradeStrategy() {
    return new TomcatRequestUpgradeStrategy();
}
like image 79
sunyi Avatar answered Sep 20 '26 12:09

sunyi