The @MessageMapping annotation in Spring routes an incoming message to a handler method based on its destination, much like @RequestMapping routes HTTP requests. It is the core of Spring's annotation-driven STOMP over WebSocket support, and it is also used for RSocket controllers.
A client sends a STOMP SEND frame to a destination such as /app/chat. Spring strips the application prefix, finds the @MessageMapping("/chat") method, invokes it, and can publish the return value to subscribers through @SendTo or SimpMessagingTemplate.
Messages travel through channels inside Spring's messaging infrastructure. Destinations starting with the configured application prefix (commonly /app) go to annotated controller methods, while destinations starting with a broker prefix (such as /topic or /queue) go to the message broker that fans them out to subscribers.
So with the prefix /app, a client sending to /app/chat reaches @MessageMapping("/chat"). The annotated method does not include the prefix.
The simple broker is in memory and suits development or small deployments. For production at scale, use enableStompBrokerRelay to relay to RabbitMQ or ActiveMQ.
A client sends JSON to /app/chat. Spring converts the payload into ChatMessage (Jackson is used for JSON), calls send, and publishes the returned object to /topic/messages, where every subscribed client receives it.
| Argument | Meaning |
|---|---|
| Payload object | The message body converted to a Java type (@Payload is implicit) |
@DestinationVariable | A value from a template such as /room/{roomId} |
@Header and @Headers | A single STOMP header or all headers |
Principal | The authenticated user for the session |
SimpMessageHeaderAccessor | Session id and other message metadata |
Here messagingTemplate is an injected SimpMessagingTemplate, which lets you publish from anywhere, not only as a method return value.
Use @SendToUser or convertAndSendToUser to reply to one user's destination, which clients subscribe to as /user/queue/replies. For errors, add a @MessageExceptionHandler method in the same controller, optionally with @SendToUser("/queue/errors"), so a failing handler does not just disappear.
WebSocket connections need their own protection. Restrict allowed origins, authenticate during the handshake or on the CONNECT frame, and authorise messages with Spring Security's message authorization (for example AuthorizationManager based message matchers). @MessageMapping also appears in RSocket controllers (@Controller with route patterns) and in Spring Cloud Stream style handlers, but the STOMP case is by far the most common.
@RequestMapping maps HTTP requests. @MessageMapping maps messages on a messaging channel, usually STOMP frames over WebSocket, and has no HTTP method or status code.
Check that the client sends to the application prefix plus the mapping (/app/chat), that @EnableWebSocketMessageBroker is present, and that the class is a @Controller found by component scanning.
No. Without it, a returned value goes to a default destination derived from the inbound one. Use @SendTo or SimpMessagingTemplate to choose the destination explicitly.
You need a broker for pub/sub to subscribers. The built-in simple broker is enough for many small applications.
@MessageMapping binds a controller method to a message destination. Combine it with a configured application prefix, @SendTo or SimpMessagingTemplate for broadcasting, @MessageExceptionHandler for errors, and Spring Security for authorisation to build real-time features over STOMP.