Building a reactive web service with Spring Webflux, Kotlin, and PostgreSQL

David Liman
3 min readMar 25, 2019

This post shows how to create a reactive web service with Spring Webflux, Kotlin, PostgreSQL

For the context, Spring Framework 5 introduced the so-called Reactive Stack. The keyword reactive refers to the Reactive Manifesto, which is a specification for asynchronous stream processing with non-blocking back-pressure. This specification is a joint collaboration between engineers from Netflix, Pivotal, Red Hat, Twitter and many others. It has been implemented in many languages such as: Java, Javascript, Swift, NET, etc.

In short, Spring Webflux is a non-blocking web framework that uses Reactor library, which implements the Reactive Streams Specifications, to asynchronously manage HTTP requests.

Requirements

Say we want to build an HTTP service that can do the following:

  • curl -XPOST /api/users/create -d '{"name": "some-name", "email": "some-email"}'
  • curl -XPOST /api/users/fetch -d '{"user_id": 123}'
  • curl -XPOST /api/users/all

Note: the final code repo is here:

Let’s start the journey by going to Spring Initializr. It is a very handy tool to generate the skeleton project.

  • Project: Gradle Project
  • Language: Kotlin
  • Spring Boot: 2.2.0 (SNAPSHOT)
  • Group: com.dvliman
  • Artifact: demo
  • Dependencies: Reactive Web
  • Generate Project — ⌘ + ⏎

At this point, if you run ./gradlew bootRun, you should see something like this:

Netty started on port(s): 8080
Started ApplicationKt in 7.113 seconds (JVM running for 9.353)

Good! Now we just need to add additional dependencies:

David Liman

Software craftsman who loves solving problems with functional paradigm. I Iook for simple solutions to complex problems