Javalin: What happens when Java marries Kotlin

The greatest thing about development and technology is that developers are never too comfortable with their existing knowledge base. They are forever learning in an attempt to bring about something better even if they are possessed of the best of software tools and information in their hands. It is therefore not surprising to learn that this keen spirit is what ultimately led to the development of Javalin.

Javalin started as a mere forked branch of the most widely used open-source framework Spark that is built over Java and Kotlin, but it quickly saw a huge uprising in adoption and transformed into a completely new framework: re-written from scratch, heavily influenced by the famous koa.js. This calls out for the similarity between the two web frameworks which is that they both belong to the modern micro web framework category which was pioneered by Sinatra. It was a welcome surprise as this goes on to mean that Ruby developers would especially find it quite familiar in syntax and execution.

A REST API

A common misconception could be that Javalin aims to be a full-fledged web framework with diverse features and applications. However, the truth is that it just acts as an incredibly lightweight REST API library. It is also usually referred to as a micro framework. Besides, it has no concept of a Model, View and Controller architecture, but it does provide full functionality and support for template engines, computer communication protocols like WebSockets, and also convenient static file serving.

In case of constructing single-page applications, Javalin enables you to create not only backend programs for RESTful API but also serves an index.html page with powerful static resources. This becomes increasingly useful and practical if you are not into the deployment of an Apache or Nginx server along with your Javalin service.

As a bonus, the simplicity of Javalinextends to even simple and traditional websites if you wish to, rather than a REST API. There are many template engine wrappers available online for quick, easy and comfortable setup.

Java and Kotlin interoperability

Like most interoperable frameworks, Javalin also has the ability to act as a Java web framework and at the same time act as a Kotlin web framework, this shows that the API is consistently being developed with a strategic focus on high interoperability between the two aforementioned web languages This is achieved by the mixing of the two in the source code itself.

While Javalin itself is written primarily in Kotlin, a few of the essential ‘core classes’ are written in Java. This has become the centre of the recipe that has provided this level of interoperability in Javalin between the two languages. Javalin is more of a transitional language towards Kotlin for companies that have traditionally been a Java-first company. And hence, moving a Javalin project from mainstream Java to a more relevant Kotlin isn’t a mess and doesn’t need a steep learning curve. This consistency of a dual nature for this API has been a constant objective of the project.

JSON responses

A more usual application of Javalin is to act as a server of JSON objects which is done quite easily by calling the ctx.json() function. This function primarily transforms the objects inside parenthesis to JSON object by using the JSON plugin offered by Javalin itself. This plugin is extensively configurable, which enables for almost all JSON libraries to be compatible with Javalin, for example, GSon, Jackson etcetera.

One more thing worth noting is that by default Javalin tends to define two separate interfaces for mapping, to and from, JSON and thus includes a Jackson form of implementation for both these mappings. But that still is mutable and can be replaced by whatever is the developer’s preference.

Filters and mappers

In a real development scenario, it often happens that the same logic or thought is put into multiple endpoints or many errors and solved and handled in the same routine manner. This is where Javalin shines with its filters and mappers. Quite how HTTP endpoints work, the filters in Javalin also happen to use the same Handler interface. Also, a Path is not necessary while attaching filters to Javalin instances.

There is also the concept of a ‘before filter’, that runs before the actual endpoint handlers. These ‘before filters’ help in restricting the endpoint handler to perform the task in some special cases as instructed by the programmer. It throws an exception and hence stops the execution. Similarly, there exists an ‘after filter’ that runs after endpoint even after the exception is already handled.

Exception mapper

A good and a common practice is to write programs that throw exceptions while writing controllers for such web apps. In case of missing or restricted resources – such as –in the event a certain file is not present or the user isn’t allowed to view or edit it, the program should throw an error and the request should be handled likewise. Javalin streamlines this process by its Exception mapper which also includes an assembled set of pre-mapped exceptions for comfort and ease. Few examples include BadRequestResponse, NotFoundReponse, and UnauthorizedResponse.

Javalin is a lightweight and powerful REST API that is built on a codebase of a few thousand lines of Jetty and thus, its performance is as good as raw Jetty code. Javalin’s small size enables its developers to reason out, discuss and develop this web framework every day – with more finesse and sturdier applicability. Any company that requires a lightweight, power-intensive and fast web application should test out Javalin.

Javalin is truly the web applications framework of the future as only few frameworks let you do completely what you want and yet provide you with seamless performance.

Article is published By Aegis Infoways, Provides Java Development Services. Contact To Hire Java Spring Boot Developer India.

Published by Sandeep Aegis

Sandeep - Working as Internet Marketing Executive in Aegisisc - Software Development Company India, Provide Software Solution services for Healthcare Industry.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: