Adding Kotlin to an existing Java project

One of the feedbacks to my talk about how Kotlin can help Java developers was that I should have shown a practical example of how to start adding Kotlin code to a Java project. So I decided to give you a small example.

I used the Java SpringBoot demo application code from one of my blog posts about. how to consume a SOAP web service with Spring Boot .

With help of the guide from the Kotlin website I made Maven compile both the Java and Kotlin sources.

Then I created the Kotlin source directory, created a file named NumberController.kt. I copied and pasted the code from the NumberController.java into this new file. IntelliJ IDEA offered me to convert the code to Kotlin automatically. The Kotlin class is visibly shorter as it got rid of the constructor boilerplate code needed by Spring dependency injection.

Finally, I added a small piece of computation to demonstrate the usage of infix operators with BigDecimal that Kotlin supports.

And that's it, check it out and play around!

The resulting code branch that you can clone at github .

Or you can just take a quick look at The comparison to the original Java-only version .

Do you want to add something? Let's discuss on Mastodon.

Back to all blog posts