Over a million developers have joined DZone.

OpenWhisk Functions With Kotlin in IntelliJ

DZone's Guide to

OpenWhisk Functions With Kotlin in IntelliJ

Building OpenWhisk functions? Here's how to develop them in Kotlin with a helping hand from IntelliJ IDEA.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

I’ve implemented some code that shows how Apache OpenWhisk functions can be developed with Kotlin. The Java code is built via Gradle and put into a Docker image, which can be deployed to OpenWhisk cloud providers like the IBM Cloud.

Get the code from GitHub.

There are different approaches to write OpenWhisk functions with Kotlin. Check out this separate article, which describes how to generate a JAR file that is directly deployed on OpenWhisk.

The approach taken in my sample is slightly different. Rather than deploying a JAR file to the OpenWhisk Java runtime, a Docker image that contains the JAR file is used instead. The advantage of this approach is that it allows us to use different Java versions and you can develop and test the same Docker image locally which is later deployed and run in the cloud. This minimizes the chances to run into issues because of different environments.

Here is a screenshot that shows the Kotlin code in IntelliJ:


The sample function can be invoked locally via this command.

$ curl --request POST \
  --url http://localhost:8080/run \
  --header 'Cache-Control: no-cache' \
  --header 'Content-Type: application/json' \  
  --data '{ "value": {"parameter1":"Niklas","parameter2":"Heidloff"}}'

This is the response of the function:

   "param1": "value1 Niklas",
   "param2": "value2"

Let’s take a look at the code:

import io.javalin.Javalin
import com.fasterxml.jackson.annotation.JsonIgnoreProperties

data class InputObject(val parameter1: String  = "", val parameter2: String  = "")
@JsonIgnoreProperties(ignoreUnknown = true)
data class InputBody(val value: InputObject = InputObject())
data class OutputObject(val param1: String, val param2: String)

fun main(args: Array<String>) {
    val app = Javalin.create().apply {
        exception(Exception::class.java) { e, ctx -> e.printStackTrace() }

    app.post("/init") { ctx -> ctx.status(200) }

    app.post("/run") { ctx ->
        val body = ctx.bodyAsClass(InputBody::class.java)
        val inputObject = body.value

        val outputObject = OutputObject("value1 " + inputObject.parameter1, "value2")

As my web framework, I’ve picked Javalin and I’ve implemented two endpoints ‘/init’ and ‘/run’. The actual implementation of the function goes in the run function.

The inputs and the outputs of the function are defined in data classes at the top. The mapping between classes and JSON data is done automatically by Kotlin/Jackson.

I like that the code has only 27 lines. The same function that I developed with Java has more than 100 lines of code.

If you want to try out OpenWhisk in the cloud, you can get an account on the IBM Cloud.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

java ,apache openwhisk functions ,kotlin ,intellij idea ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}