• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Spring Boot with Kotlin Hello World Example+ Maven

September 11, 2017 by javainterviewpoint Leave a Comment

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

Spring Boot with Kotlin

Folder Structure:

Spring Boot with Kotlin 3

  • Create a simple Spring Starter Project (File –> New –> Spring Starter Project). Select the language as “Kotlin” and Spring Boot version as “1.5.6”

Spring Boot with Kotlin 2

  • 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.

Other interesting articles which you may like …

  • Spring Boot CommandLineRunner and ApplicationRunner
  • Spring Boot – How to Change Embedded Tomcat default port
  • Spring Boot CRUDRepository Example- Spring Data JPA
  • Fix missing src/main/java folder in Eclipse Maven Project
  • Spring MVC CRUD Example with MySql + JdbcTemplate
  • RESTful Java Client With Jersey Client
  • RESTEasy Hello World Example with Apache Tomcat
  • RESTful Java client with RESTEasy client
  • Spring RESTful Web Services Hello World XML Example
  • Springfox Swagger 2 for Spring RESTful Web Services

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

Spring Boot with Kotlin 4

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.spring[email protected]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.spring[email protected]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!! 🙂

Filed Under: Java, Kotlin, Spring, Spring Boot, Spring Tutorial Tagged With: Kotlin, Spring Boot, Spring Boot with Kotlin

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·