Search⌘ K
AI Features

Converting a Webflux Request Into an RSocket Request/Response

Explore how to connect a Spring WebFlux HTTP POST request to an RSocket request/response method in Spring Boot. Understand the reactive flow from receiving the request to forwarding it over RSocket and handling the response. Learn to write integration tests using WebTestClient and MongoDB clearance to validate the client-server interaction within a reactive programming model.

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.

Java
@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 signal that we ...