@SubscribeMapping is a Spring messaging annotation that handles a client's STOMP SUBSCRIBE frame for an application destination and sends a one-time reply straight back to that client. It is the WebSocket equivalent of a request-response call, and it is used to deliver initial state, such as the current list of positions, right after a client subscribes.
The key detail is that the reply goes only to the subscribing session through the client outbound channel, not through the message broker. That makes it different from @MessageMapping combined with @SendTo, which broadcasts to every subscriber.
In a STOMP-over-WebSocket setup, Spring routes messages by destination prefix. Destinations that start with the application prefix (commonly /app) go to your annotated controller methods. Destinations that start with broker prefixes (such as /topic or /queue) go to the broker.
When a client subscribes to an application destination such as /app/portfolio/positions, Spring matches it against @SubscribeMapping("/portfolio/positions"), calls the method, and sends the return value to that client only. The subscription itself is not kept for later broadcasts, because the destination is handled by your method and not by the broker.
| Annotation | Triggered by | Delivery of the return value |
|---|---|---|
@MessageMapping | A client SEND frame | Broker, if @SendTo is present or inferred |
@SubscribeMapping | A client SUBSCRIBE frame | Directly to the subscribing client |
Restrict allowed origins to the sites that really connect. A wildcard origin lets any web page open your socket.
A browser client using STOMP.js subscribes to /app/portfolio/42/positions and receives the current positions once. It then subscribes to a broker topic such as /topic/portfolio/42/updates for live changes. The pair gives you snapshot plus stream without a separate REST call.
By default the reply is not broadcast. If you add @SendTo, the return value is published to that broker destination instead, so every subscriber of it receives the message.
Use this sparingly. It sends the message to all clients on each new subscription, which is rarely what you want for initial state.
@SubscribeMapping("/notifications") is hit by /app/notifications, not by /topic/notifications.SimpMessagingTemplate.convertAndSend.@MessageMapping handles messages a client sends and normally publishes results to a broker topic. @SubscribeMapping handles the act of subscribing and replies directly to that one client.
No. It is part of Spring's STOMP messaging support (spring-messaging) and relies on STOMP destinations and frames.
Yes. The subscription to an application destination is handled by your controller, and the broker relay is used for the broker destinations. Configure it with enableStompBrokerRelay.
Use Spring Security's messaging authorisation to restrict destinations, and validate the principal against the requested resource in the method.
@SubscribeMapping answers a STOMP subscription to an application destination with a single direct reply, which is ideal for sending initial state. Pair it with broker topics for live updates, keep the application and broker prefixes distinct, and secure each destination.