Miguel Ángel Ballesteros bio photo

Miguel Ángel Ballesteros

Maker, that uses software to build great ideas. Manager, that encourage and develop people to achieve sounded goals. Father, that loves family. Learner, never ever stop learner.

Email LinkedIn Github
RSS Feed

Vertx3 + Kotlin Tutorial for a REST API with a JBDC backend

Overview

This is a tutorial for beginners and intermediate developers that want a quick dive into asynchronous programming with Vertx and Kotlin.

Requirements

This tutorial assumes you have installed Java, Maven, Git, and as you want to taste Kotlin, you probably use IntelliJ.

Getting the code

I’ve published the tutorial code in GitHub, so create a work directory and clone the project:

git clone git@github.com:maballesteros/vertx3-kotlin-rest-jdbc-tutorial.git

After cloning the project, open the main pom.xml in IntelliJ. It’s a maven multiproject, with a module (subproject) for each step.

Step 1: Starting a Vertx simple HTTP server

In this first step just want to show how easy is to have an asynchronous HTTP server using Vertx and Kotlin… even without adding Kotlin sugar (more on this later).

 1 import io.vertx.core.Vertx
 2 import io.vertx.ext.web.Router
 3 
 4 object Vertx3KotlinRestJdbcTutorial {
 5 
 6     @JvmStatic fun main(args: Array<String>) {
 7         val vertx = Vertx.vertx()
 8         val server = vertx.createHttpServer()
 9         val port = 9000
10         val router = Router.router(vertx)
11 
12         router.get("/").handler { it.response().end("Hello world!") }
13 
14         server.requestHandler { router.accept(it) }.listen(port) {
15             if (it.succeeded()) println("Server listening at $port")
16             else println(it.cause())
17         }
18     }
19 }

First, we get a Vertx instance, and create an HttpServer from it. The server is not yet started, so we can keep configuring it to match our needs. In this case, just handle GET / and return a classical Hello world!.

Step 2: In-memory REST User repository

In the second step we define a simple User repository with the following API:

1 data class User(val id:String, val fname: String, val lname: String)
2 
3 interface UserService {
4     fun getUser(id: String): Future<User>
5     fun addUser(user: User): Future<Unit>
6     fun remUser(id: String): Future<Unit>
7 }

So, we have Users and a service to get, add, and remove them.

Notice that we’re doing asynchronous programming, so we can’t return directly a User or Unit. Instead, we must provide some kind of callback or return a Future<T> result that allows you listen to success / fail operation events.

In this step we’ll implement this service with a mutable Map (a HashMap):

 1 class MemoryUserService(): UserService {
 2 
 3     val _users = HashMap<String, User>()
 4 
 5     init {
 6         addUser(User("1", "user1_fname", "user1_lname"))
 7     }
 8 
 9     override fun getUser(id: String): Future<User> {
10         return if (_users.containsKey(id)) Future.succeededFuture(_users.getOrImplicitDefault(id))
11         else Future.failedFuture(IllegalArgumentException("Unknown user $id"))
12     }
13 
14     override fun addUser(user: User): Future<Unit> {
15         _users.put(user.id, user)
16         return Future.succeededFuture()
17     }
18 
19     override fun remUser(id: String): Future<Unit> {
20         _users.remove(id)
21         return Future.succeededFuture()
22     }
23 }

Futhermore, we have to handle typical REST operations GET, POST, and DELETE.

Notice:

  • the call to router.route().handler(BodyHandler.create()), so we can fetch the request body as a String.
  • the use of Gson to encode / decode JSON
  • how to listen to future resolution (line 38: future.setHandler)
 1 object Vertx3KotlinRestJdbcTutorial {
 2     val gson = Gson()
 3 
 4     @JvmStatic fun main(args: Array<String>) {
 5         val port = 9000
 6         val vertx = Vertx.vertx()
 7         val server = vertx.createHttpServer()
 8         val router = Router.router(vertx)
 9         router.route().handler(BodyHandler.create())
10         val userService = MemoryUserService()
11 
12         router.get("/:userId").handler { ctx ->
13             val userId = ctx.request().getParam("userId")
14             jsonResponse(ctx, userService.getUser(userId))
15         }
16 
17         router.post("/").handler { ctx ->
18             val user = jsonRequest<User>(ctx, User::class)
19             jsonResponse(ctx, userService.addUser(user))
20         }
21 
22         router.delete("/:userId").handler { ctx ->
23             val userId = ctx.request().getParam("userId")
24             jsonResponse(ctx, userService.remUser(userId))
25         }
26 
27         server.requestHandler { router.accept(it) }.listen(port) {
28             if (it.succeeded()) println("Server listening at $port")
29             else println(it.cause())
30         }
31     }
32 
33     fun jsonRequest<T>(ctx: RoutingContext, clazz: KClass<out Any>): T =
34         gson.fromJson(ctx.bodyAsString, clazz.java) as T
35 
36 
37     fun jsonResponse<T>(ctx: RoutingContext, future: Future<T>) {
38         future.setHandler {
39             if (it.succeeded()) {
40                 val res = if (it.result() == null) "" else gson.toJson(it.result())
41                 ctx.response().end(res)
42             } else {
43                 ctx.response().setStatusCode(500).end(it.cause().toString())
44             }
45         }
46     }
47 }

Step 3: In-memory REST User repository (with simplified REST definitions)

In the third step we’ll just simplify REST definitions. We’ll spend some time mapping backend services to REST endpoints, so the easier, the best.

Let’s compare two valid code samples. The first one is our current codebase, and the second one, what we would like to achieve:

 1 router.get("/:userId").handler { ctx ->
 2     val userId = ctx.request().getParam("userId")
 3     jsonResponse(ctx, userService.getUser(userId))
 4 }
 5 
 6 router.post("/").handler { ctx ->
 7     val user = jsonRequest<User>(ctx, User::class)
 8     jsonResponse(ctx, userService.addUser(user))
 9 }
10 
11 ---------->
12 
13 get("/:userId") { send(userService.getUser(param("userId"))) }
14 
15 post("/") { send(userService.addUser(bodyAs(User::class))) }

So, we want to get rid of boilerplate code such router., .handler { ctx -> , and ctx.request().getParam(). This code just obfuscate what we try to express in the REST API definitions. This is particularly evident when there’s a bunch of business packages with lots of REST endpoints each. Then, the simpler the definitions, the better the maintenance tasks.

How do we get that cleaner and much more expressive code? Of course, with Kotlin sugar for DSL definitions. You can find the key idea at Type Safe Builders in the main Kotlin site. We use those ideas and define the following extension methods:

 1 val GSON = Gson()
 2 
 3 fun HttpServer.restAPI(vertx: Vertx, body: Router.() -> Unit): HttpServer {
 4     val router = Router.router(vertx)
 5     router.route().handler(BodyHandler.create())  // Required for RoutingContext.bodyAsString
 6     router.body()
 7     requestHandler { router.accept(it) }
 8     return this
 9 }
10 
11 fun Router.get(path: String, rctx:RoutingContext.() -> Unit) = get(path).handler { it.rctx() }
12 fun Router.post(path: String, rctx:RoutingContext.() -> Unit) = post(path).handler { it.rctx() }
13 fun Router.put(path: String, rctx:RoutingContext.() -> Unit) = put(path).handler { it.rctx() }
14 fun Router.delete(path: String, rctx:RoutingContext.() -> Unit) = delete(path).handler { it.rctx() }
15 
16 fun RoutingContext.param(name: String): String =
17     request().getParam(name)
18 
19 fun RoutingContext.bodyAs<T>(clazz: KClass<out Any>): T =
20         GSON.fromJson(bodyAsString, clazz.java) as T
21 
22 fun RoutingContext.send<T>(future: Future<T>) {
23     future.setHandler {
24         if (it.succeeded()) {
25             val res = if (it.result() == null) "" else GSON.toJson(it.result())
26             response().end(res)
27         } else {
28             response().setStatusCode(500).end(it.cause().toString())
29         }
30     }
31 }

Step 4: JDBC backend REST User repository

In the fourth step we add a JDBC backend. This requires some infrastructure code to keep things simple.

Let’s checkout the JDBC implementation:

 1 class JdbcUserService(private val client: JDBCClient): UserService {
 2 
 3     init {
 4         client.execute("""
 5         CREATE TABLE USERS
 6             (ID VARCHAR(25) NOT NULL,
 7             FNAME VARCHAR(25) NOT NULL,
 8             LNAME VARCHAR(25) NOT NULL)
 9         """).setHandler {
10             val user = User("1", "user1_fname", "user1_lname")
11             addUser(user)
12             println("Added user $user")
13         }
14     }
15 
16     override fun getUser(id: String): Future<User> =
17         client.queryOne("SELECT ID, FNAME, LNAME FROM USERS WHERE ID=?", listOf(id)) {
18             it.results.map { User(it.getString(0), it.getString(1), it.getString(2)) }.first()
19         }
20 
21 
22     override fun addUser(user: User): Future<Unit> =
23         client.update("INSERT INTO USERS (ID, FNAME, LNAME) VALUES (?, ?, ?)",
24                 listOf(user.id, user.fname, user.lname))
25 
26 
27     override fun remUser(id: String): Future<Unit> =
28         client.update("DELETE FROM USERS WHERE ID = ?", listOf(id))
29 }

Easy right? Note that we must provide a JDBCClient at construction time. Here’s the code added to the main() to build the JDBC client and connect it to a real database:

1 val client = JDBCClient.createShared(vertx, JsonObject()
2         .put("url", "jdbc:hsqldb:mem:test?shutdown=true")
3         .put("driver_class", "org.hsqldb.jdbcDriver")
4         .put("max_pool_size", 30));
5 val userService = JdbcUserService(client)
6 // val userService = MemoryUserService()

In this tutorial we use hsqldb, a Java database frequently used in db layer testing, as it provides an in-memory implementation that comes handy.

Vertx JDBC support doesn’t come with so simple APIs. Again, some Kotlin extension methods and functional programming help to keep things simple (look at db_utils.kt).

Step 5: JDBC backend REST User repository (with Promises and more Kotlin sugar)

In the fifth step we add more infrastructure code to simplify even more, and let the beast scale better with complexity.

In previous examples we’ve used the Future<T> type provided by Vertx. It gives you a quite familiar way to subscribe to future results, so whenever it is available, you can query it for success or failure and take any additional action.

But the Future<T> type lacks some important features that are very important to scale out of the simple examples shown here:

  • Composability: you cannot chain Future<T> types
  • Synchronization: you cannot wait to several futures to finish, and act after the last one.

Well, I’m not completely fair: you can, indeed… but with a lot of boilerplate code that would give you an unmanageable code.

So, what’s the alternative? The Promise pattern solves all this, and is a the facto standard for handling asynchronous code.

First, we need a Promise implementation in Kotlin that hooks to the Vertx event loop.

Then we can redefine our codebase on this asynchronous pattern. Let’s start by redefining the service API (easy, just change Future<T> to Promise<T>):

1 data class User(val id:String, val fname: String, val lname: String)
2 
3 interface UserService {
4 
5     fun getUser(id: String): Promise<User?>
6     fun addUser(user: User): Promise<Unit>
7     fun remUser(id: String): Promise<Unit>
8 }

The service JDBC implementation it’s also quite similar. Note the change in the init() method, where we start using the composition operations .pipe() and .then() to chain asynchronous actions in a quite semantic way:

 1 class JdbcUserService(private val client: JDBCClient): UserService {
 2 
 3     init {
 4         val user = User("1", "user1_fname", "user1_lname")
 5         client.execute("""
 6             CREATE TABLE USERS
 7                 (ID VARCHAR(25) NOT NULL,
 8                 FNAME VARCHAR(25) NOT NULL,
 9                 LNAME VARCHAR(25) NOT NULL)
10             """)
11         .pipe { addUser(user) }
12         .then { println("Added user $user") }
13     }
14 
15     override fun getUser(id: String): Promise<User?> =
16         client.queryOne("SELECT ID, FNAME, LNAME FROM USERS WHERE ID=?", listOf(id)) {
17             User(it.getString(0), it.getString(1), it.getString(2))
18         }
19 
20 
21     override fun addUser(user: User): Promise<Unit> =
22         client.update("INSERT INTO USERS (ID, FNAME, LNAME) VALUES (?, ?, ?)",
23                 listOf(user.id, user.fname, user.lname)).then {  }
24 
25 
26     override fun remUser(id: String): Promise<Unit> =
27         client.update("DELETE FROM USERS WHERE ID = ?", listOf(id)).then {  }
28 }

We use:

  • .then(): when the next action returns an immediate result.
  • .pipe(): when the next action returns a Promise<T> also, and we want to chain on it.

In JavaScript promises you just have a .then() operation, but Java being typed requires to distinguish both cases.

Promises pattern simplify not only the user code, but also the infrastructure code. Compare the future based infrastructure for database with the promise based one. As you can see, promises mix well with functional code.


In this step we simplified even more the REST API definition:

 1 val dbConfig = JsonObject()
 2         .put("url", "jdbc:hsqldb:mem:test?shutdown=true")
 3         .put("driver_class", "org.hsqldb.jdbcDriver")
 4         .put("max_pool_size", 30)
 5 
 6 object Vertx3KotlinRestJdbcTutorial {
 7 
 8     @JvmStatic fun main(args: Array<String>) {
 9         val vertx = promisedVertx()
10 
11         val client = JDBCClient.createShared(vertx, dbConfig);
12         val userService = JdbcUserService(client)
13 
14         vertx.restApi(9000) {
15 
16             get("/:userId") { send(userService.getUser(param("userId"))) }
17 
18             post("/") { send(userService.addUser(bodyAs(User::class))) }
19 
20             delete("/:userId") { send(userService.remUser(param("userId"))) }
21 
22         }
23     }
24 }

This gives us a simpler view of the exposed REST API, removing all the boilerplate code to extension methods:

 1 fun Vertx.restApi(port: Int, body: Router.() -> Unit) {
 2     createHttpServer().restApi(this, body).listen(port) {
 3         if (it.succeeded()) println("Server listening at $port")
 4         else println(it.cause())
 5     }
 6 }
 7 
 8 fun HttpServer.restApi(vertx: Vertx, body: Router.() -> Unit): HttpServer {
 9     val router = Router.router(vertx)
10     router.route().handler(BodyHandler.create())  // Required for RoutingContext.bodyAsString
11     router.body()
12     requestHandler { router.accept(it) }
13     return this
14 }
15 
16 fun Router.get(path: String, rctx:RoutingContext.() -> Unit) = get(path).handler { it.rctx() }
17 fun Router.post(path: String, rctx:RoutingContext.() -> Unit) = post(path).handler { it.rctx() }
18 fun Router.put(path: String, rctx:RoutingContext.() -> Unit) = put(path).handler { it.rctx() }
19 fun Router.delete(path: String, rctx:RoutingContext.() -> Unit) = delete(path).handler { it.rctx() }

Summing up

In this tutorial we saw how to build a simple asynchronous REST API using Vertx and Kotlin. We started with a simple “Hello world!” HTTP server, and ended with a real asynchronous REST API leveraging good Kotlin sugar and the Promise pattern for asynchronous programming.