Loading...
Spring Framework Reference Documentation 7.0.2의 Dots as Separators의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
메시지가 @MessageMapping 메서드로 라우팅될 때, AntPathMatcher와 매칭됩니다. 기본적으로 패턴은 구분자로 슬래시 (/)를 사용하는 것으로 예상합니다. 이는 웹 애플리케이션에서 좋은 컨벤션이며 HTTP URL과도 유사합니다. 그러나 메시징 컨벤션에 더 익숙하다면, 구분자로 dot (.)을 사용하도록 전환할 수 있습니다.
다음 예제는 그 방법을 보여줍니다:
1@Configuration 2@EnableWebSocketMessageBroker 3public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer { 4 5 // ... 6 7 @Override 8 public void configureMessageBroker(MessageBrokerRegistry registry) { 9 registry.setPathMatcher(new AntPathMatcher(".")); 10 registry.enableStompBrokerRelay("/queue", "/topic"); 11 registry.setApplicationDestinationPrefixes("/app"); 12 } 13}
1@Configuration 2@EnableWebSocketMessageBroker 3class WebSocketConfiguration : WebSocketMessageBrokerConfigurer { 4 5 // ... 6 7 override fun configureMessageBroker(registry: MessageBrokerRegistry) { 8 registry.setPathMatcher(AntPathMatcher(".")) 9 registry.enableStompBrokerRelay("/queue", "/topic") 10 registry.setApplicationDestinationPrefixes("/app") 11 } 12}
1<beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:websocket="http://www.springframework.org/schema/websocket" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 https://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/websocket 8 https://www.springframework.org/schema/websocket/spring-websocket.xsd"> 9 10 <websocket:message-broker application-destination-prefix="/app" path-matcher="pathMatcher"> 11 <websocket:stomp-endpoint path="/stomp"/> 12 <websocket:stomp-broker-relay prefix="/topic,/queue" /> 13 </websocket:message-broker> 14 15 <bean id="pathMatcher" class="org.springframework.util.AntPathMatcher"> 16 <constructor-arg index="0" value="."/> 17 </bean> 18 19</beans>
그 후, 컨트롤러는 @MessageMapping 메서드에서 구분자로 dot (.)을 사용할 수 있으며, 다음 예제가 이를 보여줍니다:
1@Controller 2@MessageMapping("red") 3public class RedController { 4 5 @MessageMapping("blue.{green}") 6 public void handleGreen(@DestinationVariable String green) { 7 // ... 8 } 9}
1@Controller 2@MessageMapping("red") 3class RedController { 4 5 @MessageMapping("blue.{green}") 6 fun handleGreen(@DestinationVariable green: String) { 7 // ... 8 } 9}
이제 클라이언트는 /app/red.blue.green123로 메시지를 전송할 수 있습니다.
앞의 예제에서, 우리는 “broker relay”의 프리픽스를 변경하지 않았는데, 이는 그것들이 전적으로 외부 메시지 브로커에 의존하기 때문입니다. 사용하는 브로커에 대해 destination 헤더에 대해 어떤 컨벤션을 지원하는지 보려면 해당 브로커의 STOMP 문서 페이지를 참조하십시오.
반면에, “simple broker”는 설정된 PathMatcher에 의존하므로, 구분자를 전환하면 그 변경은 브로커와 브로커가 메시지의 destination을 구독의 패턴과 매칭하는 방식에도 적용됩니다.
Connecting to a Broker
Authentication