liveBook · Manning

@messagemapping

As explained in Spring in Action, Sixth Edition

@MessageMapping is a Spring annotation that maps messages sent to a destination route to a handler method.

@MessageMapping maps an incoming RSocket message to a Spring controller method. Unlike @GetMapping or @PostMapping, it matches a destination route such as "greeting" rather than an HTTP path. section 14.2.1

A mapped RSocket handler can:

  • Receive a reactive payload such as Mono<String> and return another Mono containing the response. section 14.2.1
  • Use route placeholders such as "greeting/{name}" and read their values with @DestinationVariable. section 14.2.1
  • Handle reactive RSocket communication through Spring controllers and Reactor's Mono or Flux types. section 14.4

Greeting request-response handler

A controller can map the "greeting" route to handleGreeting(Mono<String>). The method logs the received greeting and maps it to a response such as "Hello back to you!". A client sends data with RSocketRequester by selecting the same route, supplying the payload, and retrieving a Mono response. section 14.2.1

Listing 14.2 A simple RSocket request-response server
package rsocket;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
 
@Controller
@Slf4j
public class GreetingController {
 
    @MessageMapping("greeting")
    public Mono<String> handleGreeting(Mono<String> greetingMono) {
        return greetingMono
            .doOnNext(greeting -> 
                log.info("Received a greeting: {}", greeting))
            .map(greeting -> "Hello back to you!");
    }
    
}

Browse a complete GreetingController showing the annotation, reactive request payload, logging, and response transformation. section 14.2.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains route matching, reactive payloads, destination variables, and client calls, while deferring broader RSocket communication models.
Examples
Demonstrates greeting routes, personalized name placeholders, GreetingController, and RSocketRequester client requests.
What it assumes
Assumes basic Spring controller and reactive-type familiarity while contrasting RSocket routes with HTTP request mappings.
What you can do after
Apply it via Spring RSocket to build reactive route handlers that receive messages and return responses.
How it connects
Connects @MessageMapping with RSocket, Spring controllers, RSocketRequester, Reactor Mono, Flux, and @DestinationVariable.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.