Play JSON API 生成的错误消息类型示例:
scala> import play.api.libs.json._
import play.api.libs.json._
scala> Json.obj("k" -> Json.obj("m" -> 7))
res0: play.api.libs.json.JsObject = {"k":{"m":7}}
scala> (res0 \ "p").as[String]
play.api.libs.json.JsResultException: JsResultException(errors:List((,List(ValidationError(validate.error.expected.jsstring,WrappedArray())))))
at play.api.libs.json.JsValue$$anonfun$2.apply(JsValue.scala:67)
at play.api.libs.json.JsValue$$anonfun$2.apply(JsValue.scala:67)
at play.api.libs.json.JsResult$class.fold(JsResult.scala:69)
at play.api.libs.json.JsError.fold(JsResult.scala:10)
at play.api.libs.json.JsValue$class.as(JsValue.scala:65)
at play.api.libs.json.JsUndefined.as(JsValue.scala:98)
at .<init>(<console>:12)
at .<clinit>(<console>)
at .<init>(<console>:7)
// gazillion lines more
scala> (res0 \ "k" \ "m").as[String]
play.api.libs.json.JsResultException: JsResultException(errors:List((,List(ValidationError(validate.error.expected.jsstring,WrappedArray())))))
at play.api.libs.json.JsValue$$anonfun$2.apply(JsValue.scala:67)
at play.api.libs.json.JsValue$$anonfun$2.apply(JsValue.scala:67)
at play.api.libs.json.JsResult$class.fold(JsResult.scala:69)
at play.api.libs.json.JsError.fold(JsResult.scala:10)
at play.api.libs.json.JsValue$class.as(JsValue.scala:65)
at play.api.libs.json.JsNumber.as(JsValue.scala:108)
at .<init>(<console>:12)
at .<clinit>(<console>)
at .<init>(<console>:7)
// gazillion lines more
有没有办法从此 API 中获得更好的错误消息?例如,上述两种情况的错误消息可能如下No value found at JsPath \ "p"
所示Value found at JsPath \ "k" \ "m" cannot be read as type String
。