Christian Schwoerer
12/18/2020, 11:57 AM{
"value1": 20,
"value2": 3.33
}
The result should look like this:
{
"result": 66.6
}
Am I doing this right?
@Post("/multiply")
fun multiply(): String {
val objectMapper = createObjectMapper()
val requestBody = KotlessContext.HTTP.request.body?.let { String(it, UTF_8) }
val request = objectMapper.readValue(requestBody, Request::class.java)
val result = request.value1 * request.value2
return objectMapper.writeValueAsString(Response(result))
}
data class Request(
val value1: BigDecimal,
val value2: BigDecimal
)
data class Response(
val result: BigDecimal
)
private fun createObjectMapper() = ObjectMapper().findAndRegisterModules().setSerializationInclusion(NON_NULL)
Is this the only way to extract the body and create a JSON response? Or ist there anything more convenient like @RequestBody
or to create the JSON response?TanVD
12/18/2020, 12:37 PMfun multiply(value1: Double, value2: Double)
?Christian Schwoerer
12/18/2020, 1:04 PMTanVD
12/18/2020, 1:34 PMChristian Schwoerer
12/18/2020, 1:41 PMHunter Kelly
03/04/2021, 4:23 PM