In the tutorial, we will learn how to build a simple Hello World application using Spring Boot with Kotlin. Kotlin is a programming language created by JetBrains. It is an object-oriented language including many ideas from functional programming and runs on top of the JVM. In order to build our Kotlin HelloWorld example, we will be using STS (Spring Tool Suite) with Kotlin Plugin and Maven.
Kotlin Plugin
As a pre-requisite, we need to install “Kotlin Plugin for Eclipse 0.8.2”. The latest updated plugin is available in the below location. The Kotlin Plugin for Eclipse helps you write, run, debug and test programs in Kotlin language.
https://dl.bintray.com/jetbrains/kotlin/eclipse-plugin/last/
To install the plugin, go to Help –> Install New Software and give the above URL in “Work with” field, now select all the Kotlin tools and click on Finish.
Spring Boot with Kotlin
Folder Structure:
- Create a simple Spring Starter Project (File –> New –> Spring Starter Project). Select the language as “Kotlin” and Spring Boot version as “1.5.6”
- Now add the following dependency in the POM.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javainterviewpoint</groupId> <artifactId>SpringBootKotlin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringBootKotlin</name> <description>Spring Boot Kotlin with Maven</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <kotlin.version>1.1.4-3</kotlin.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jre8</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> <version>${kotlin.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>kotlin-maven-plugin</artifactId> <groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version> <configuration> <compilerPlugins> <plugin>spring</plugin> </compilerPlugins> <jvmTarget>1.8</jvmTarget> </configuration> <executions> <execution> <id>compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <phase>test-compile</phase> <goals> <goal>test-compile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-allopen</artifactId> <version>${kotlin.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
- Create a Kotlin classes SpringBootKotlinApplication.kt under com.javainterviewpoint.kotlin folder.
SpringBootKotlinApplication.kt
Add the below code in SpringBootKotlinApplication.kt
package com.javainterviewpoint.kotlin import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication @SpringBootApplication class SpringBootKotlinApplication fun main(args: Array<String>) { SpringApplication.run(SpringBootKotlinApplication::class.java, *args) println(" **** Hello World *****") }
Running:
Select the Project –>Run As –> Run Configuration –>Maven –> New. In the Main tab, key in the Goals as “spring-boot:run” and click on Run
Output:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
2017-09-10 13:34:30.202 INFO 496 --- [ main] c.j.k.SpringBootKotlinApplicationKt : Starting SpringBootKotlinApplicationKt on Jack-PC with PID 496 (D:\Jackson\sts-3.8.3.RELEASE\Workspace\SpringBootKotlin\target\classes started by Jack in D:\Jackson\sts-3.8.3.RELEASE\Workspace\SpringBootKotlin)
2017-09-10 13:34:30.205 INFO 496 --- [ main] c.j.k.SpringBootKotlinApplicationKt : No active profile set, falling back to default profiles: default
2017-09-10 13:34:30.286 INFO 496 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@249fcf46: startup date [Sun Sep 10 13:34:30 IST 2017]; root of context hierarchy
2017-09-10 13:34:31.164 INFO 496 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-09-10 13:34:31.187 INFO 496 --- [ main] c.j.k.SpringBootKotlinApplicationKt : Started SpringBootKotlinApplicationKt in 1.484 seconds (JVM running for 8.057)
**** Hello World *****
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.954 s
[INFO] Finished at: 2017-09-10T13:34:31+05:30
[INFO] Final Memory: 31M/264M
[INFO] ------------------------------------------------------------------------
2017-09-10 13:34:31.490 INFO 496 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@249fcf46: startup date [Sun Sep 10 13:34:30 IST 2017]; root of context hierarchy
2017-09-10 13:34:31.492 INFO 496 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
We have run our first Kotlin Hello World application. Happy Learning!! 🙂
Leave a Reply