This tutorial demonstrates using Kotlin within WoOF.
While the tutorial is simple, it is entirely written in Kotlin.
As Kotlin compiles to classes, much of the existing features of WoOF can be used as is. Furthermore, as this tutorial also demonstrates, Kotlin functions can be used.
To enable using Kotlin, add the following to your pom.xml:
<dependency> <groupId>net.officefloor.kotlin</groupId> <artifactId>officekotlin</artifactId> </dependency>
The Kotlin to service the request is:
package net.officefloor.tutorial.kotlinhttpserver import net.officefloor.web.ObjectResponse fun service(request: KotlinRequest, response: ObjectResponse<KotlinResponse>) { response.send(KotlinResponse("Hello ${request.name} from Kotlin")) }
As part of supporting Kotlin, the necessary Jackson support is included to enable sending/receiving Kotlin data classes as JSON.
The request / response data classes are as follows:
@HttpObject data class KotlinRequest(val name: String)
data class KotlinResponse(val message: String)
As Kotlin is integrated well with Java, the following demonstrates writing tests with Kotlin:
@RegisterExtension @JvmField public val server = MockWoofServerExtension() @Test fun service() { val response = this.server.send(MockWoofServer.mockJsonRequest(HttpMethod.POST, "/", KotlinRequest("Daniel"))); response.assertJson(200, KotlinResponse("Hello Daniel from Kotlin")); }
JUnit 4 example:
@Rule @JvmField public val server = MockWoofServerRule() @Test fun service() { val response = this.server.send(MockWoofServer.mockJsonRequest(HttpMethod.POST, "/", KotlinRequest("Daniel"))); response.assertJson(200, KotlinResponse("Hello Daniel from Kotlin")); }
The next tutorial covers Scala.