Writing a Spring Application using Kotlin
For a webproject I recently did, that basically had no technological constraints, I decided to go for Kotlin. Kotlin has the nice advantage of running within the JVM, thus having access to everything that also runs in it. Plus of course, to be able to use one of the powerful Java-based build systems, like Gradle, Maven, and the likes.
To see faster initial results, I went for the Spring Boot Webstarter, which it turns out doesn’t play quite as nice with Kotlin. For the record: The correct way would have been to use the Kotlin-based Kara Web Framework, but that of course still wouldn’t work with the other types of Spring Boot Starters. And also just for the lulz of it, I wanted to see if the statement of having access to everything in the JVM holds true.
Solution
Originally I started to document the journey I undertook to get it all running and then while searching for linkable pages I found out someone else had already solved this half a year before me, in an even better and more elegant way. Naturally, like such things always go, I couldn’t have come across that post when I initially encountered the problem. ;-)
So go read it, i’ll wait.
As I already mentioned the upper one basically matches my solution and but the lower one is the actually interesting one. It still didn’t work for me, so here’s the corrected version:
package tld.example.package
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
@EnableAutoConfiguration
@SpringBootApplication
open class Application
fun main(args: Array<String>) {
SpringApplication.run(Application::class.java, *args)
}
As they also mention in their updated post that’s pretty much all you need to do to get it up and running with recent versions of Kotlin.