Fork me on GitHub

R2DBC Tutorial

This tutorial demonstrates using R2DBC logic within WoOF.

It is a simple application to retrieve data from a database. As such, the configuration for this tutorial is the following:

ReactorHttpServer screen shot.

Tutorial Source

Reactive logic

The following is the R2DBC logic to retrieve the data:

	public Mono<Message> retrieveData(@HttpPathParameter("id") String id, R2dbcSource source) {
		return source.getConnection()
				.flatMap(connection -> Mono.from(connection.createStatement("SELECT CONTENT FROM MESSAGE WHERE ID = $1")
						.bind(0, Integer.parseInt(id)).execute()))
				.flatMap(result -> Mono.from(, metadata) -> {
					String content = row.get(0, String.class);
					return new Message(content);

The R2dbcSource provides access to obtain a connection.

To enable the returned Reator types to be handled natively by OfficeFloor, add the following:


OfficeFloor will subscribe to the returned Mono. The success is passed as parameter to the next procedure (sending it as response):

	public void send(@Parameter Message message, ObjectResponse<Message> response) {


To have the R2DBC available, add the following:

		<!-- Also include appropriate R2DBC libraries for particular database -->

then configure as follows in application.objects:

	<managed-object source="net.officefloor.r2dbc.R2dbcManagedObjectSource">
		<property-file path="" />

with example properties:



The following shows invoking the R2DBC code that asynchronously retrieves data:

	public final MockWoofServerExtension server = new MockWoofServerExtension();

	public void getData() {
		MockWoofResponse response = this.server.send(MockWoofServer.mockRequest("/message/1"));
		response.assertJson(200, new Message("TEST"));


The next tutorial covers using Cats Effect.