Optional WebSocket engine presence in WebFlux config
Closes gh-22587
This commit is contained in:
parent
689adb4bd0
commit
e02ccc9374
|
@ -65,7 +65,9 @@ import org.springframework.web.reactive.result.method.annotation.ResponseBodyRes
|
|||
import org.springframework.web.reactive.result.method.annotation.ResponseEntityResultHandler;
|
||||
import org.springframework.web.reactive.result.view.ViewResolutionResultHandler;
|
||||
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||
import org.springframework.web.reactive.socket.WebSocketHandler;
|
||||
import org.springframework.web.reactive.socket.server.WebSocketService;
|
||||
import org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService;
|
||||
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebExceptionHandler;
|
||||
|
@ -435,17 +437,29 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
|||
|
||||
@Bean
|
||||
public WebSocketHandlerAdapter webFluxWebSocketHandlerAdapter() {
|
||||
WebSocketService service = getWebSocketService();
|
||||
WebSocketHandlerAdapter adapter = (service != null ?
|
||||
new WebSocketHandlerAdapter(service) : new WebSocketHandlerAdapter());
|
||||
WebSocketHandlerAdapter adapter = new WebSocketHandlerAdapter(initWebSocketService());
|
||||
|
||||
// For backwards compatibility, lower the (default) priority
|
||||
// Lower the (default) priority for now, for backwards compatibility
|
||||
int defaultOrder = adapter.getOrder();
|
||||
adapter.setOrder(defaultOrder + 1);
|
||||
|
||||
return adapter;
|
||||
}
|
||||
|
||||
private WebSocketService initWebSocketService() {
|
||||
WebSocketService service = getWebSocketService();
|
||||
if (service == null) {
|
||||
try {
|
||||
service = new HandshakeWebSocketService();
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// Don't fail, test environment perhaps
|
||||
service = new NoUpgradeStrategyWebSocketService();
|
||||
}
|
||||
}
|
||||
return service;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected WebSocketService getWebSocketService() {
|
||||
return null;
|
||||
|
@ -535,4 +549,13 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
private static final class NoUpgradeStrategyWebSocketService implements WebSocketService {
|
||||
|
||||
@Override
|
||||
public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler webSocketHandler) {
|
||||
return Mono.error(new IllegalStateException("No suitable RequestUpgradeStrategy"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue