From f795f252d1caf7a300b0d8cca1d36c8ddc766057 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 11 Jun 2021 14:30:13 -0400 Subject: [PATCH] Fix Websocket dynamic registration mapping order Related to https://stackoverflow.com/questions/67923303/dynamic-registration-of-websocket-output-adapter-is-not-working By default the `AbstractHandlerMapping` comes with the `order = Ordered.LOWEST_PRECEDENCE` which sorts added mappings to the end of chain. At the same time Spring Boot registers a `SimpleUrlHandlerMapping` as a fallback for all not handled requests leaving our own mapping behind consideration * Add `order = 0` to the `IntegrationDynamicWebSocketHandlerMapping` bean registration to let it to be consulted before `SimpleUrlHandlerMapping` * Add a note into `web-sockets.adoc` that `@EnableWebsocket` would disable Spring Integration dynamic WebSocket endpoints --- .../WebSocketIntegrationConfigurationInitializer.java | 7 ++++++- src/reference/asciidoc/web-sockets.adoc | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketIntegrationConfigurationInitializer.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketIntegrationConfigurationInitializer.java index f8d51b2c5df..c50af9ea47a 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketIntegrationConfigurationInitializer.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/WebSocketIntegrationConfigurationInitializer.java @@ -99,7 +99,12 @@ private void registerEnableWebSocketIfNecessary(BeanDefinitionRegistry registry) BeanDefinitionReaderUtils.registerWithGeneratedName( new RootBeanDefinition(IntegrationDynamicWebSocketHandlerMapping.class, - IntegrationDynamicWebSocketHandlerMapping::new), + () -> { + IntegrationDynamicWebSocketHandlerMapping dynamicWebSocketHandlerMapping = + new IntegrationDynamicWebSocketHandlerMapping(); + dynamicWebSocketHandlerMapping.setOrder(0); + return dynamicWebSocketHandlerMapping; + }), registry); BeanDefinitionBuilder enableWebSocketBuilder = diff --git a/src/reference/asciidoc/web-sockets.adoc b/src/reference/asciidoc/web-sockets.adoc index 77369da48f8..ce7509e1f49 100644 --- a/src/reference/asciidoc/web-sockets.adoc +++ b/src/reference/asciidoc/web-sockets.adoc @@ -429,4 +429,6 @@ dynamicServerFlow.destroy(); ==== NOTE: It is important to call `.addBean(serverWebSocketContainer)` on the dynamic flow registration to add the instance of `ServerWebSocketContainer` into an `ApplicationContext` for endpoint registration. -When a dynamic flow registration is destroyed, the associated `ServerWebSocketContainer` instance is destroyed, too, as well as the respective endpoint registration, including URL path mappings. \ No newline at end of file +When a dynamic flow registration is destroyed, the associated `ServerWebSocketContainer` instance is destroyed, too, as well as the respective endpoint registration, including URL path mappings. + +IMPORTANT: The dynamic Websocket endpoints can only be registered via Spring Integration mechanism: when regular Spring `@EnableWebsocket` is used, Spring Integration configuration backs off and no infrastructure for dynamic endpoints is registered. \ No newline at end of file