Kotlin idioms in Jooby

Overview

Jooby wasn’t original designed as a Kotlin framework, but thanks to the power and expressiveness of Kotlin we were able to write a nice and tiny module that let you write idiomatic Kotlin in Jooby.

Bootstrap idiom

The bootstrap idiom comes in two flavors:

run function:

f

Kooby class:

The run idiom is nice to quickly bootstrap an application, but doesn’t work well if you want to write unit and/or integration tests.

Request idiom

The request idiom give us implicit access to request functions and properties. So, instead of:

Just need:

Works for body and form-submits too:

param, header and body idioms

Previous examples used the Java API for accessing request objects. The request idiom comes with a couple of reified functions:

Nice ugh?

Route idiom

The route idiom group two or more routes under a common path, like:

Class idiom

The class idiom is a small enhancement that allow you to provide a Kotlin class reference while adding MVC routes, requiring dependencies, etc…

Just write: Pet::class where a class reference is required, instead of Pet::class.java

Conclusion

As said before Jooby wasn’t a Kotlin framework but luckily enough the kotlin module close the gap and help you write idiomatic Kotlin.

Here is another example of a minimal HTTP API using the Jackson and Jdbi modules:

In addition to the kotlin module, Jooby comes with two starter projects:

That’s all for now. Happy Kotlin coding!

Related articles: