Javalin 2.4 hits the spot for Kotlin and Java developers
Javalin doesn’t believe in wasting time. This lightweight web framework has had four updates in four months. We take a look at what’s up with this beautiful mix of Java and Kotlin: new classes, improved Java support, and WebSocket improvements.
More library than framework, Javalin has a fast release cycle. There have already been four version updates since we last checked in to this lightweight web framework in August! So, what do all these updates add up to? Lots of new and interesting features for Java and Kotlin developers.
Let’s dive right in, shall we?
Javalin 2.4 offers developers the chance to use the framework with an application server or servlet container like Tomcat or Weblocic with the
EmbeddedJavalin.createServlet(). Javalin uses the same internal method for
create.Servlet() as it does for Jetty.
However, developers should keep an eye out if they use this method and also call on
EmbeddedJavalin, as Jetty might throw exceptions and errors. Mind your server methods and manually exclude Jetty from your build files for this approach.
As for Jetty itself, 2.4 offers extended support for wrapped and nested Jetty handlers. Additionally, the
SessionHandler is now validated before starting the Server and the “Jetty is listening…” message now includes the context path to make things clearer for developers. It’s also possible to add a custom
HandlerCollection to the Jetty instance; Javalin is added at the end of the collection.
Also in Javalin 2.4, developers can now easily add WebSocket loggers! By calling
app.wsLogger(), the method takes a
WsHandler and can be used to log events of all types. The logger runs after the WebSocket handler for the endpoint.
There are also improvements for exception mapping. Developers can add a custom exception handler, overriding the built in handler for
HttpResponseException. Additionally, instead of a plaintext String, the default server error for uncaught exceptions uses the
SEE MORE: Javalin 2.0 is here: This lightweight web framework for Kotlin and Java developers just got better
Other Javalin updates
There’s improved support for Java 9+ projects. All of Javalin’s tests run against JDK8, JDK9, JDK10, and JDK11.
App attributes can be registered on the Javalin instances by calling
app.attribute(Class, Object). These attributes can be retrieved on the instance by calling
app.attribute(Class), or inside Handlers by calling
Additionally, a new class has arrived – the
Validator. While it does sound like a Silver Age comic book hero, it’s actually a class for validating user input. If validation fails, Javalin will throw a
BadRequestResponse with a lovely little error message.
Architecture-wise, there are two classes for the
Validator– This is the base class. It has three validation methods:
check(). It also has methods for turning itself into a
TypedValidator– When the String has been validated, additional type-safe checks can be performed by calling
check()on this class.
For more information, check out the Javalin 2.2 release notes.
Want to try out Javalin for yourself? This open source web framework is available on GitHub or here for download. Javalin is a true micro Java REST API library and has no external dependencies other than the embedded web-server, but it has plugins for JSON mapping and template rendering available if necessary.