I'm trying to write a nice Kotlin wrapper for a web framework against kotlin 1.0.3. In that I am trying to mixin a function to the request to have it return a bean via a JSON transformation using jackson.
So in my library I have the following
private val mapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule())
fun <T : Any> Request.asDataBean(type: KClass<T>): T = mapper.readValue(this.body(), type.java)
But when I goto use the code as such
post("/hello", { req, res ->
val bean = req.asDataBean(TestBean::class)
})
It errors saying that the expected value of bean is Any. What I want is for my API to work as above where whatever the generic "class" definition that is passed into asDataBean method is the type of value that is returned back.
I've also tried
fun <T> Request.asDataBean(type: KClass<*>): T = mapper.readValue(this.body(), type.java) as T
as well as changing the usage code to
val bean: TestBean = req.asDataBean(TestBean::class)
in hopes of making it work but they also give the exact same error when using the code.
How do I get it to use the generic defined by the class type passed in as the parameter (very similar to how all the spring api's work in java)?