Spring boot: Java vs Kotlin way

Janani Subbiah
4 min readJan 25, 2020
Photo by Jason Dent on Unsplash

Though google was one of the first companies to announce full support for Kotlin in Android, I think other frameworks have come a LONG way in providing first class support for Kotlin out of the box. Spring boot is one of them! Developing Spring boot, in my opinion is so much fun in itself and adding Kotlin to the mix only makes it double FUN!

If you are a Java dev looking to switch over to Kotlin this blog post is for you! Because I think some of the Spring folks have done a wonderful job of providing (and maintaining) code samples/tutorials, I am just going to link out to some of them at the end of my post that should provide good resources for anyone looking to get started with Spring and Kotlin (consider yourself warned: There are no code samples in this post!).

The Main class and method

In the Java world there would be one main class annotated with the @SpringBootApplication with a main method that starts the app up. In Kotlin this translates to one file with two components: One class annotated with @SpringBootApplication (which would be the equivalent of the main class in Java except it would not have any methods) and one stand alone function that would be responsible for actually running the main class (which would be the equivalent of the main method inside the main class in Java). So why don’t we have a main class with a static main method in Kotlin? Short answer is because Koltin does not have the concept of static variables. The long answer is it is possible to get the equivalent of Java static variables in Kotlin using the @JvMStatic annotation with a companion object. But we don’t want to do this because this way it is a lot more clean and readable.

kotlin-spring plugin

All classes in Kotlin are final by default and given spring uses proxying for annotation like @Transation and @Configuration, it was not going to be possible to use annotations unless all those classes were marked with an open keyword (which does the opposite of making classes as final). The kotlin-spring plugin is a wrapper on top of the kotlin-allopen plugin. But the good news is all of this is automatically generated by Spring Inititalizr when you specify Kotlin as your preferred language choice.

kotlin-jpa plugin

This plugin applies only if you are using JPA in your spring boot app. The kotlin-jpa plugin is a wrapper on top of the kotlin-noarg plugin that allows for generation of a no arguments constructor for classes that are marked with specific annotations. So the kotlin-jpa plugin makes it possible to generate no argument constructors for classes including the ones annotated with Hibernate’s @Entity annotation.

Kotlin DSL

Recently, I did a little research on the different ways we could use Kotlin with a Spring boot app to develop an API endpoint that would return some string value. There were three options:

  • The good old controller way using the @RestController/@Contoller annotations with @GetMapping etc.
  • Using router and handler functions. But the dependencies would still carry the @Component annotation but the controller would be replaced by a RouterFunction and each of the routes would delegate to the handler functions to actually “get stuff done”.
  • The functional Kotlin DSL way (using spring-Kofu): Not until later into the project did I realize one big key component I missed about spring Kofu: It does NOT use reflection or CGLIB proxying. The java version (spring-jafu) uses Graalvm to kind of accomplish this. I cannot speak much to how exactly this works (I am still researching into this and would love to hear the under-the-hood workings of how this is accomplished) but I will say the code reads wonderfully IF you are familiar with the Spring ecosystem and terms. Once it clicks it really sticks!

Kotlin Data classes

Remember all the getters/setters/equals/hashcode/toString methods you had to generate in your java classes for most of your POJOs? You can bid farewell to ALL those methods because Kotlin provides what are called data classes that will auto generate these methods for you. Which also means you have very concise looking POJOs (or POKOs)!

Reactor Kotlin Extensions

Project reactor provides very easy to use and developer friendly extensions (as its own gradle dependency). Refer to their GitHub page on how to integrate this into your project.

Test names

Simple things are the best! Kotlin allows for naming tests like you would write a sentence in English. An example of a test name would be `Testing Fetch All Users`. With that we can say bye-bye to long test names with a zillion underscores!

Spring’s continued support for Kotlin has really made the switch from Java Spring to Kotlin Spring smooth. As with most other things, there is some excellent documentation about the Spring’s support for Kotlin that I highly recommend reading if you are looking to make the switch!

--

--

Janani Subbiah

Product Architect | Ice cream lover | Newbie gardener