Guide to writing Java EE applications with Kotlin

Last Updated:  January 20, 2020 | Published: August 4, 2019

The precise and clean style of writing code with Kotlin is tempting as a Java developer. In addition, switching to Kotlin with a Java background is rather simple. But what about using Kotlin for an existing Java EE application or start using it for a new project? Read this blog post to get a first impression of what it takes to use Kotlin alongside Java EE in your projects.

The following technologies are used in this example: Kotlin 1.3.41, Java 11, Java EE 8, MicroProfile 2.2 running on a dockerized Open Liberty 19.0.0.7 with Eclipse OpenJ9.

Maven project setup for Kotlin

The classic Java EE Maven pom.xml  needs just two adjustments: the kotlin-stdlib dependency and the kotlin-maven-plugin:

For JPA we have to configure the kotlin-maven-noarg plugin to add parameterless constructors to our JPA entities. In addition, I'm activating the incremental Kotlin compiler  in the properties sections to decrease repetitive build times, which is optional.

Pitfalls for Java EE using Kotlin

The two most common pitfalls for Java EE with Kotlin are non-final classes & methods and parameterless constructors. Whilst Kotlin makes classes per default final, we need non-final classes for some specifications to work. This is required for example to create proxies and enrich the functionality of public methods (e.g. EJB's transaction management).

With the following sample project, you'll see where and how to solve these requirements. The project provides a REST interface to retrieve books. The JAX-RS resource class looks like the following:

The first thing to pay attention is the injection of other beans. Here I'm injecting the BookService to retrieve both all books and a book by its id. To avoid cumbersome null-checks we have to use lateinit to tell the Kotlin compiler that this instance is injected during runtime. Normally, properties declared as having a non-null type must be initialized in the constructor within Kotlin.

Next, I'm using Book? as the return type of the method to get a book by its id. This is required, as there might be no book in the database for the provided id. The Elvis operator ?: makes the required null-check and returns an HTTP 404 status in case of null.

The corresponding BookService is a singleton EJB and delegates access to the database using the EntityManager:

The open keyword on class- and method-level is required to make both non-final.

Writing JPA entities

For writing JPA entities we can utilize Kotlin's data classes. The only thing to keep in mind is that we need a public no-arg constructor for JPA to work. Enabling the JPA plugin in the Maven build section, makes sure we get a parameterless constructor for all our @Entity classes and don't have to worry:

Final thoughts for Java EE with Kotlin

Converting an existing Java EE application written in Java to Kotlin might not be that simple. You have to make sure that dependency injection, interceptors in general and your JPA entities are working. With this blog post, you might get a first impression of where to pay attention when using Kotlin. If the precise and elegant style of writing source code with Kotlin overweighs the additional pitfalls for you, then give it a try!

You can find the whole example on GitHub.

Further resources on this topic:

Have fun using Kotlin for your Java EE project,

Phil

>