Lightweight Kotlin Microservices with Javalin

There is a lot happening in the JVM space when it comes to microservices development. You have Spring Cloud thriving, Microprofile entering the stage, Vert.x letting you get reactive and Dropwizard being actively developed. What if you want something really simple though? And maybe with some Kotlin? For that you have Javalin!

Introducing Javalin

Javalin is a micro-framework for building simple REST APIs for Java and Kotlin. It comes with embedded Jetty server and is very easy to use.

This simplicity makes Javalin a very enjoyable framework for learning Kotlin. If you are new to Kotlin, the last thing you want to do is to be overwhelmed by a new opinionated framework. You want to focus on the core language features.

How do you get started with Javalin then? You need to add a relevant dependency:

Exception and Error Mapping – These help you deal with your Exceptions on the top-level.

Lifecycle Events – If you need to hook to SERVER_STARTING, SERVER_STARTED and similar events.

Server Setup – For setting up the embedded Jetty

Once again- the simplicity and clarity of these are exemplary. You really get all the basics necessary and the rest is left for you to deal with.

Packaging an executable JAR

One thing that could be improved upon in the framework is dealing with the creation of executable JAR. At the moment you have to do it yourself by adding appropriate Maven plugin. I have used the following:

I think it would be better if Javalin dealt with this problem in a similar fashion to Spring Boot, where this is taken care of for the developer.

Update: I got a response from Javalin Twitter Account on this subject: ”(…) The reason why Javalin doesn’t concern itself with Jar creation is because it’s not strictly related to Javalin. If people learn how to do it the Maven/Gradle way, this knowledge will be useful for them in future (non Javalin) projects.” – This highlights the focus and the philosophy behind the project. I can see why adding this JAR generation may be against the spirit of the project.

Jackson and SLF4J do not come included

One thing that can be also a bit of catch is lack of Jackson and SLF4J implementations included. I understand the reasons for not including them, and the framework makes it clear in the logs it provides that these are required.

I have used the following dependencies and got everything to work nicely:

The project codebase is actively maintained as a Github Repo with regular commits being made. Javalin is written in Kotlin. and there are only 10 contributors, making it an interesting project to get involved in. Apache license makes it a safe choice.

Summary

Javalin is a fascinating micro-framework. I really see it as a sort of NodeJS of the JVM. Something that is needed with the abundance of opinionated and heavy-weight frameworks out there.

I focused here on using Javalin with Kotlin, but it also works perfectly fine with Java- hence the name!

If you are already using Javalin, please- share your experiences in the comments!

Share this:

Related

2 thoughts on “Lightweight Kotlin Microservices with Javalin”

Good intro – it would be interesting to see comparisons with the other Kotlin framworks out there. How does Javalin compare to the other pure-Kotlin web micro-libraries such as Ktor, http4k, hexagon and kovert? How well do they take advantage of the much terser Kotlin API features? How (de)composable are the various concepts (middleware etc) for reuse? How easy are these frameworks to test? Performance compared to heavyweight contenders such as Spring?

Post navigation

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About

E4developer is a place where I share my open and honest views on software development, technology and working with people. The name – e4 comes from a chess move, this is how I start most of my games. Follow me on twitter – @e4developer