post function in your example requires the route: (Request, Response) -> Any parameter route: (Request, Response) -> Any , which is a function that accepts a request and response and returns some non-zero value.
When you use a lambda expression as route , its return type is inferred from the last lambda body expression, and since in Kotlin an assignment is not an expression, the following lambda does not have a return type in all:
{ req, res -> val bean = req.asDataBean(TestBean::class) }
To make it work, just make the bean last expression
{ req, res -> val bean = req.asDataBean(TestBean::class) bean }
or not use the assignment at all:
{ req, res -> req.asDataBean(TestBean::class) }
Note. I used the following asDataBean function asDataBean :
fun <T: Any> Request.asDataBean(type: KClass<T>): T = mapper.readValue(this.body(), type.java)
You can also do a reified reboot, which causes an unoriented one, so you do not need to expose all internal elements:
inline fun <reified T: Any> Request.asDataBean(): T = asDataBean(T::class) req.asDataBean<TestBean>() // usage
source share