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") } }