MicroProfile and Kotlin

by Hayri Cicek


Posted on 18-Nov-2018

Kotlin is an object oriented programming language created by JetBrains which runs on top of the JVM and can also be compiled to JavaScript or native code using LLVM.
In this tutorial we will use Kotlin language to create a simple MicroProfile REST api.

Tools You Will Need
Maven 3.3+
Your favorite IDE.
JDK 1.8+

Clone Project


$ git clone https://github.com/cicekhayri/microprofile-kotlin-rest-start.git


Update pom.xml
Open the pom.xml and in the dependencies section add the following:


<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>${kotlin.version}</version>
</dependency>


and inside build section add the following


<plugins>
    <plugin>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-maven-plugin</artifactId>
        <version>${kotlin.version}</version>
        <executions>
            <execution>
                <id>compile</id>
                <phase>process-sources</phase>
                <goals>
                    <goal>compile</goal>
                </goals>
            </execution>
            <execution>
                <id>test-compile</id>
                <phase>process-test-sources</phase>
                <goals>
                    <goal>test-compile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>


The kotlin-maven-plugin compiles the Kotlin source code and modules.
your pom.xml should look like this :


<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.kodnito</groupId>
    <artifactId>microprofile-kotlin-rest</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <kotlin.version>1.3.10</kotlin.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.eclipse.microprofile</groupId>
            <artifactId>microprofile</artifactId>
            <version>2.0.1</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>restapi</finalName>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>process-test-sources</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    
    </build>
</project>


Creating a Todo Data Class
The next step is to create a Todo Data Class that has two properties: task and description.
Inside com.kodnito.restapi.rest create Todo.kt file and add the following


package com.kodnito.restapi.rest

data class Todo(
        val task: String,
        val description: String
)


Create the HelloEndpoint
Inside com.kodnito.restapi.rest create HelloEndpoint.kt file and add the following :


package com.kodnito.restapi.rest

import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import kotlin.collections.List
import java.util.Arrays


@Path("/hello")
class HelloEndpoint {

    @GET
    @Produces(MediaType.TEXT_HTML)
    fun hello(): String {
        return "Hello From Microprofile Kotlin"
    }
    
    @GET
    @Path("/todos")
    @Produces(MediaType.APPLICATION_JSON)
    fun getTodos(): List<Todo> {
        return Arrays.asList(
                Todo("Learn Kotlin", "Learn More Kotlin"),
                Todo("Buy Milk", "Buy milk for coffeee")
        )
    }
}


@Path annotation identifies the URI path to which the resource responds.
@Produces annotation will automatically convert the response to JSON format
@GET annotation maps /todos HTTP GET request to getTodos() function.

Run the service
The first thing to do is to download Payara Micro
Create lib folder inside the project directory and move the payara-micro-5.183.jar to the lib directory and run the following commands to start the service:


$ mvn clean package && java -jar lib/payara-micro-5.183.jar --deploy target/restapi.war


Now open your browser and navigate to http://localhost:8080/restapi/hello and you should see a simple message:



and now visit http://localhost:8080/restapi/hello/todos and see a list of todos:

Your project directory should look like this:

tree .
.
├── lib
│   └── payara-micro-5.183.jar
├── pom.xml
└── src
    └── main
        └── kotlin
            └── com
                └── kodnito
                    └── restapi
                        └── rest
                            ├── ApplicationConfig.kt
                            ├── HelloEndpoint.kt
                            └── Todo.kt

Clone the final project from GitHub


$ git clone https://github.com/cicekhayri/microprofile-kotlin-rest-finished.git


In this tutorial we learned how to create a simple REST api using MicroProfile and Kotlin.


Share this: