Created
October 2, 2019 21:57
-
-
Save krishnabhargav/1a61413d6dceaebaf7f43a8af7cde00f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import infrastructure.Json | |
import io.javalin.Javalin | |
import io.javalin.plugin.json.FromJsonMapper | |
import io.javalin.plugin.json.JavalinJson | |
import io.javalin.plugin.json.ToJsonMapper | |
sealed class Device { | |
data class Laptop(val model: String) : Device() | |
data class Phone(val model: String, val carrier: String): Device() | |
} | |
class Employee(val device: List<Device>) | |
fun main() { | |
val app = Javalin.create().start(7000) | |
JavalinJson.toJsonMapper = object : ToJsonMapper { | |
override fun map(obj: Any): String = Json.toJson(obj) | |
} | |
JavalinJson.fromJsonMapper = object : FromJsonMapper { | |
override fun <T> map(json: String, targetClass: Class<T>): T = Json.fromJsonWithClass(json, targetClass) | |
} | |
app.get("/") { ctx -> | |
ctx.result("Hello, World!") | |
} | |
app.get("/device") { ctx -> | |
val devices = listOf(Device.Laptop("Macbook Pro"), Device.Phone("IPhone", "T-Mobile")) | |
ctx.json(Employee(devices)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment