...

/

Converting a Webflux Request Into an RSocket Request/Response

Converting a Webflux Request Into an RSocket Request/Response

Learn how to forward a new item over RSocket and create a test class using the autoconfigured WebTestClient.

Forwarding Item using request/response

Check out the code below to see how we can connect an incoming HTTP POST with WebFlux to our RSocket request/response method on the server.

@PostMapping("/items/request-response") //1
Mono<ResponseEntity<?>> addNewItemUsingRSocketRequestResponse(@RequestBody Item item) {
return this.requester
.flatMap(rSocketRequester -> rSocketRequester
.route("newItems.request-response") //2
.data(item) //3
.retrieveMono(Item.class)) //4
.map(savedItem -> ResponseEntity.created( //5
URI.create("/items/request-response")).body(savedItem));
}
Forwarding a new Item over an RSocket using request/response

Here’s a breakdown of the code above:

  1. In line 1, @PostMapping(...) indicates that this Spring WebFlux method accepts HTTP POST requests at the /items/request-response route.

  2. In line 5, we can route() this request to the destination newItems.request-response by flat mapping over the Mono<RSocketRequestor>.

  3. In line 6, we submit the payload of the Item object in the data() method.

  4. In line 7, we ...