The rest api JS client can send both int and string as the value of some field.
{
field1: "123",
field2: "456"
}
{
field1: 123,
field2: 456
}
Here is a game action with the case class to which you need to transform the body of the json request:
case class Dto(field1: Int, field2: Int)
object Dto {
implicit val reads = Json.reads[Dto]
}
def create = Action.async(BodyParsers.parse.json) { implicit request =>
request.body.validate[Dto].map {
dto => someService.doStuff(dto).map(result => Ok(Json.toJson(result)))
}.recoverTotal {
e => jsErrorToBadRequest(e)
}
}
In case I send json values ββwith int values, it works fine. But in case field1 or field2 are strings ("123", "456"), it fails because request.body.validate expects an Int.
But the problem is that the JS client sends the values ββfrom the input fields, and the input fields are converted to strings.
What is the best way to handle both ints and strings? (So ββthis action should convert json to dto in both cases)