zaterdag 26 augustus 2017

spring boot service with Kotlin

Introduction

Kotlin is a fairly new programming language orignally developed by jetbrains and these days supported on android by Google. Also since spring 5.0 is it possible to write kotlin programs in spring  (also spring boot applications). Kotlin runs on the jvm and is easily compatible with excisting java classes.  


The jvm it runs on at the moment I am writing this, is Java 6. No, dont worry the clever part is, that the team behind Kotlin found ways to make the features up to Java 8 available in te last version of Kotlin (1.1.4). The style of programming is more the kind of functional programming. 

Unfortunally I cannot cover Kotlin complete in this post but It will give you a rough Idea How it works and how powerfull it is. 

Lets play with some examples: 

Example 1: Hello world  


fun main(args: Array<String>) {
    println("hello world")
}

A view small points that should get our attention:

  1. There is no class definition around the main method.
  2. The declartion of the function is not void but fun
  3. the args arguments is not an String[] array but an Array type.
  4. The definition of the args is first the name : type.
  5. The println() command. In java: System.out.println().
in this small example we allready see a lot of differences which actually
lead to less code lines (lazy programmers like me love that).
The way the args types are declared, also goes for the declaration
of the var types some examples:
val double: Double = 10.0 // 64 bit
val float: Float = 10.0F // or f, 32 bit
val long: Long = 10L // 64 bit
val int: Int = 10 // 32 bit
val hexadecimal = 0x0F // Prefix '0x'
val binary = 0b01 // Prefix '0b'
Example 2 calling a java class:
// The java class
public class Test {

    public String hello() {
        return "Hello";
    }
}

// The Kotlin code:

//To declare a var you use var. To declare a constant you use val.
// Here we declare the java class defined above.
var test = Test();

fun main(args:Array<String>) {
    //Here we call the hello method declared in the Test class.
    println(test.hello());
}

Example 3: the spring boot service:

The best part of this is that the project can be an 
maven project as usual or any other build tool you desire.


The pom file needs:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>LATEST_VERSION</version>
</parent>

The code looks like follows:

//The main function comparable with what we know from Java.
//The point of interest is that the main function needs to be
//declared outside the class application. Otherwise the method
// will not be seen as a main method.
fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}

// The class declaration simular to what we know in Java 
//with some small adjustements. By default a class is final
//in Kotlin. To undo this we need the keyword open. Spring Boot
// requires that.
@SpringBootApplication
open class Application() {
}
// finally the rest controller It has all the characteristics 
//I described earlier. Like the fun definition of the function etc.
 For the rest it is code as we know it from Spring.
@RestControllerclass CustomerController {

    @RequestMapping(value =  "/test")
    fun test(): String {
        return   "Hello world";
    }

}

To make this example work create an .kt file and
compile it with:
 kotlinc your_file.kt -include-runtime -d your_file.jar 
and run it with:
java -jar your_file.jar
Or use an ide that does this kind of work for you (leasy me again)
Conclusion:
With Kotlin we produce significant less lines of code and still have the power that we used to. Important is that Kotlin has a wide range of support from compagnies and frameworks which make our lives as programmers easier. I hope you will play a bit with it and enjoy it as much As I did. At last but not least I would like to thank Shaun Thomas from Sytac for giving a wonderfull presentation about Kotlin which enlightened me to write this post.
Have fun!