Getting started with Javalin

With the popularity of Spring Boot and the great support for integrating with different types of frameworks, it’s often hard to imagine using something different than Spring. However, there are numerous frameworks out there which provide similar features for building applications like Microservices, and Javalin is one of those frameworks. This post will help you to get started with Javalin.

(The image above is representing Aboriginal Australians at a spear-throwing contest in Arnhem Land, in the Northern Territory. Woomeras (Like an atalatl) are used in order to throw the spears more effectively. (Photo by Three Lions)

What is Javalin

Javalin is a web framework for building Java and Kotlin web applications. In this regard, Javalin is similar to frameworks like NodeJS (see Koajs.com) or SparkJava.The focus of Javalin is on creating lightweight REST APIs.

Creating a Gradle build for Javalin

In this blogpost we’ll create a small hello world application based on Gradle Kotlin. and a follow up blogpost will go into the testing of our app.

To get started with Javalin, we need to have a Javalin dependency. An easy way to do that is to use Gradle. Assuming you have Gradle installed, we can create a new project with the following command:

gradle init --type java-library --dsl kotlin

(Answer the questions prompted, their values aren’t really important for the purpose of this guide.)

Once the project has been initialised, open the build.gradle.kts file, and replace the contents with the following content:

// build.gradle.kts
plugins {
    val kotlinVersion = "1.3.21"
    id("org.jetbrains.kotlin.jvm") version kotlinVersion
    application
}

application {
    // Define the main class for the application and add Kt for Kotlin classes
    mainClassName = "HelloWorldKt"
}

dependencies {
    compile("io.javalin:javalin:2.8.0")
    compile("org.slf4j:slf4j-simple:1.7.26")

    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
}

repositories {
    jcenter()
}

The main library used here is the Javalin 2.8.0 library, which at this time of writing is the latest one. After this, navigate to src/main/kotlin, and create a HelloWorld.kt file. This file will contain the contents of our complete application.

// src/main/kotlin/HelloWorld.kt
import io.javalin.Javalin

fun main() {
    val app = Javalin.create().start(7000)
    app.get("/") { ctx -> ctx.result("Hello World") }
}

Now, running gradlew run from a terminal will start up the Javalin application, running on port 7000. Opening http://localhost:7000 in your browser will result in the root route being called (app.get("/")), which will display the Hello World text in your browser. Now you’re ready to include more functionality into your app and to write a test for it, which I’ll dive into in one of the next blogposts.

Older Post
Newer Post

Leave a Reply

Your email address will not be published. Required fields are marked *