@MessageMapping marks a method in a Spring controller as the handler for messages sent to a given destination over WebSocket with the STOMP sub-protocol. It plays the role for messaging that @RequestMapping plays for HTTP: the destination string is matched against incoming STOMP SEND frames and the method is invoked with the payload.
It belongs to org.springframework.messaging.handler.annotation and is used together with @EnableWebSocketMessageBroker, a message broker and, usually, @SendTo or SimpMessagingTemplate to push results back to clients.
With STOMP over WebSocket, a client opens one connection and then sends frames with a destination header. Spring routes them by prefix:
/app) go to @MessageMapping methods in @Controller classes./topic or /queue) go to the message broker, which delivers them to subscribers.So a client sending to /app/chat reaches the method annotated @MessageMapping("/chat"). The /app prefix is stripped before matching.
/ws is the HTTP endpoint clients connect to for the WebSocket handshake. Restrict allowed origins to the sites that really need access. The simple broker is in-memory and fine for one instance; for several instances or durability, use enableStompBrokerRelay with RabbitMQ or ActiveMQ.
A client sends JSON to /app/chat. Spring converts it to ChatMessage with the configured message converter (Jackson by default), calls handle, and publishes the return value to /topic/messages, where every subscriber receives it.
A JavaScript client using the @stomp/stompjs library:
@MessageMapping methods support a flexible set of arguments:
| Argument | Purpose |
|---|---|
@Payload (or an unannotated object) | The converted message body |
@DestinationVariable | A path variable from the destination, as in /room/{roomId} |
@Header and @Headers | A single header or all headers |
Principal | The authenticated user of the WebSocket session |
SimpMessageHeaderAccessor | Session id and other STOMP details |
Here messaging is an injected SimpMessagingTemplate. Use it when the method sends to a computed destination or returns void. Use @SendToUser or convertAndSendToUser to reply to one user only.
You can add @Valid to the payload parameter to apply Bean Validation. Handle exceptions with @MessageExceptionHandler in the same controller (or a @ControllerAdvice), optionally with @SendToUser("/queue/errors") to return the error to the sender.
For security, apply Spring Security's message authorisation (AuthorizationManager<Message<?>> through MessageMatcherDelegatingAuthorizationManager) so destinations are protected like HTTP routes. Do not rely on the client choosing the right destination.
@RequestMapping maps HTTP requests. @MessageMapping maps STOMP messages by destination and has no HTTP method, status code or response in the HTTP sense.
No. If the method returns a value and has no @SendTo, Spring publishes it to the input destination prefixed with /topic by default. Use @SendTo to choose the target explicitly, or return void and use SimpMessagingTemplate.
Not in the usual way. It relies on the messaging infrastructure behind @EnableWebSocketMessageBroker. For raw WebSocket handling, implement a WebSocketHandler.
WebFlux uses reactive WebSocket handlers, and Spring RSocket uses @MessageMapping for RSocket routes. The STOMP flow described here is for Spring MVC.
@MessageMapping routes STOMP messages sent to a destination to a controller method. Configure the application prefix and a broker, accept a typed payload, reply with @SendTo or SimpMessagingTemplate, and secure destinations with Spring Security.