Creating a RSocket Client

Learn how to configure a client-side REST controller.

Configuring a client-side REST controller

Let’s create a WebFlux controller that will take incoming HTTP requests and forward them to the back-end server over its RSocket connection. That’s perhaps not as sophisticated as, say, a template that uses WebSockets to communicate from the browser to the backend. That said, learning how RSockets work makes for an effective example.

We’ll do this by creating a class called RSocketController in our newly-minted rsocket-client application in the com.greglturnquist.hackingspringboot package as follows:

@RestController //1
public class RSocketController {
private final Mono<RSocketRequester> requester; //2
public RSocketController(RSocketRequester.Builder builder) { //3
this.requester = builder
.dataMimeType(APPLICATION_JSON) //4
.metadataMimeType(parseMediaType(MESSAGE_RSOCKET_ROUTING.toString())) //5
.connectTcp("localhost", 3000) //6
.retry(5) //7
.cache(); //8
}
}
Configuring a client-side REST controller
  1. In line 1, @RestController signals that this class is used for an API not rendering HTML.

  2. In line 3, Mono<RSocketRequestor> is a Reactor wrapper around Spring Framework’s ...