【发布时间】:2022-01-07 05:12:26
【问题描述】:
我们有以下示例,似乎提取器在将 Json 转换为案例类时不起作用。
import play.api.libs.json.Reads._
import play.api.libs.json._
import play.api.libs.json.Format.GenericFormat
val json: JsValue = Json.parse("""
{
"firstName" : "John",
"lastName" : "Doe"
}
""")
trait BasePublicForm {
def firstName: String
def lastName: String
}
case class CustomerPublicForm(firstName: String, lastName: String) extends BasePublicForm
case class LeadPublicForm(firstName: String, lastName: String ) extends BasePublicForm
object CustomerPublicForm {
implicit val writesPublicLeadFormRequest: Writes[CustomerPublicForm] = Json.writes[CustomerPublicForm]
implicit val readsPublicLeadFormRequest: Reads[CustomerPublicForm] = Json.reads[CustomerPublicForm]
def apply(firstName: String, lastName: String): CustomerPublicForm = {
if(firstName.equalsIgnoreCase("John")) {
throw new Exception("John Exception")
}
new CustomerPublicForm(firstName, lastName)
}
}
object LeadPublicForm {
def apply(firstName: String, lastName: String): LeadPublicForm = {
new LeadPublicForm(firstName, lastName)
}
}
val s = json.validate[CustomerPublicForm] match {
case JsSuccess(form, _) => {
form
// do something with place
}
case e: JsError => {
// error handling flow
throw new Exception("Error")
}
}
s
链接 -> https://scastie.scala-lang.org/eZrHTOVkQvSUJmoAJMTXfQ
任何想法为什么它没有按预期返回Exception?
【问题讨论】: