@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
