I’m just starting with Lagom (and Scala) and I created a service to add books and persist them. One thing that I couldnt find is how you can make JSON parameters optional but still assign a default value to it?
I have a service to Post a Book:
namedCall("/api/postBooks", postBooks _)
Which maps on:
def postBooks(): ServiceCall[List[Book], Done]
To make a JSON parameter optional so that it does not throw an error when it is not provided in the JSON, I am using the Option parameter which also works:
case class Book(titleName: String, publishingDate: Option[Int] = Some(0))
(I limited to two parameters here to make it more readable)
I would have assumed that adding “= Some(0)” would add a default parameter when there is no publishingDate in the JSON sent to my service, but this is not the case. When publishingDate is not added, it will not have a value in my Book object.
Any idea how I can add a default value when it was not provided by the User in the JSON?