Building a CRUD application with RSocket and Spring

Image for post
Image for post

In the last post, we explored the basic RSocket support in Spring and Spring Boot. In this post, we will create a CRUD application which is closer to the real world applications.

We will create a client and server applications to demonstrate the interactions between RSocket client and server side.

Firstly let’s create the server application.

You can simply generate a project template from Spring initializr, set the following properties.

  • Build: Maven
  • Java: 11
  • Spring Boot version: 2.3.0.M3(I preferred the new version for practicing new techniques)
  • Dependencies: RSocket, Spring Data R2dbc, H2 Database, Lombok

If you are new to Spring Data R2dbc, check the post Accessing RDBMS with Spring Data R2dbc.

In the server application, we will use RSocket to serve a RSocket server via TCP protocol.

Open the src/main/resources/application.properties, add the following properties.

Like what I have done in the former posts, firstly create a simple POJO.

And create a simple Repository for Post.

Create a Controller class to handle the request messages.

Create a schema.sql and a data.sql to create tables and initialize the data.

Note: In the Spring 2.3.0.M3, Spring Data R2dbc is merged in the Spring Data release train. But unfortunately, the auto-configuration of ConnectionFactoryInitializer is NOT ported.

To make sure the the schema.sql and data.sql are loaded and executed at the application startup, declare a ConnectionFactoryInitializer bean yourself.

Start the server application.

Now let’s move to the client application.

Similarly, generate a project template from Spring Initializr, in the Dependencies area, ensure you have chosen WebFlux, RSocket, Lombok.

The client application is a generic Webflux application, but uses RSocketRequester to shake hands with the RSocket server.

Declare a RSocketRequester bean to connect localhost:7000 via the TCP protocol.

Create a generic RestController and use the RSocketRequester bean to communicate with the RSocket server.

Create a POJO Post to present the RSocket message payload that transferred between the client and server side.

Start up the client application.

Try to test the CRUD operations by curl.

As a bonus, try to add a filter to find the posts by keyword.

In the server application, create a new method in the PostRepository.

And in the PostController , create a new route to handle this request from client.

In the client side, change the PostClientController 's all method to the following:

Now, try to add an extra title request parameter to access http://localhost:8080/posts.

Get the source codes from my Github.

Written by

Self-employed technical consultant, solution architect and full-stack developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store