An RSocket server is a TCP endpoint that accepts requests on configured routes and returns reactive responses. In Spring, create one with a controller, much like a web or REST controller, but map messages with @MessageMapping instead of HTTP paths. 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!");
}
}
A minimal controller receives a Mono<String>, logs the greeting, and maps it to a response. section 14.2.1
Build the endpoint by combining three pieces:
- Annotate a controller method with @MessageMapping("greeting") to handle messages sent to the greeting route. section 14.2.1
- Accept the incoming payload as Mono<String> and use map() to create the response Mono. section 14.2.1
- Set spring.rsocket.server.port, such as 7000, so clients know where to connect. section 14.2.1
- Add route variables such as greeting/{name}, then read them with @DestinationVariable. section 14.2.1
Client connection
An RSocketRequester connects over TCP with requesterBuilder.tcp("localhost", 7000), sends data through route("greeting"), and retrieves a typed Mono response. section 14.2.1
