为什么我会收到“应用程序不带参数"在Play Framework 2.3中使用JSON Read? [英] Why I get "Application does not take parameters" using JSON Read with Play framework 2.3?
问题描述
我想在Play框架2.3x中为一些Scala模型类编写JSON验证.我正在使用JSON Reads按照说明进行操作( https://playframework.com/documentation/2.3.x/ScalaJsonCombinators ).但是我收到应用程序不带参数"错误,并且我不知道如何解决此问题.
I wanna write JSON validation for a few Scala model classes in Play framework 2.3x. I'm using JSON Reads to do that following the instructions (https://playframework.com/documentation/2.3.x/ScalaJsonCombinators). But I get "Application does not take parameters" error and I don't know how to fix this.
这是我的代码.
package models
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
import reactivemongo.bson.BSONObjectID
import java.util.Date
case class ArtifactModel(
_id: BSONObjectID,
name: String,
createdAt: Date,
updatedAt: Date,
attributes: List[AttributeModel],
stateModels: List[StateModel])
case class AttributeModel(
name: String,
comment: String)
case class StateModel(
name: String,
comment: String)
object ArtifactModel {
implicit val artifactModelReads: Reads[ArtifactModel] = (
(__ \ "_id").readNullable[String] ~
(__ \ "name").read[String] ~
(__ \ "createdAt").readNullable[Long] ~
(__ \ "updatedAt").readNullable[Long] ~
(__ \ "attributes").read[List[AttributeModel]] ~
(__ \ "stateModels").read[List[StateModel]]
)(ArtifactModel) // here is the error: "Application does not take parameters"
implicit val attributeModelReads: Reads[AttributeModel] = (
(__ \ "name").read[String] ~
(__ \ "comment").read[String]
)(AttributeModel)
implicit val stateModelReads: Reads[StateModel] = (
(__ \ "name").read[String] ~
(__ \ "comment").read[String]
)(StateModel)
}
你能帮我吗?对于Scala/Play中JSON验证的任何解决方案或建议,均表示赞赏.
Can you help me? Any solution or suggestions for JSON validation in Scala/Play are appreciated.
推荐答案
Reads对象的类型与apply方法采用的类型不同.例如,readNullable[String]
结果为Option[String]
,而不是String
. BSONObjectId
和Date
相同.这样可以编译,但是您可能需要使用一些地图:
The types of the Reads object are not the same as those the apply method takes. E.g., readNullable[String]
results Option[String]
, not String
. Same for the BSONObjectId
and the Date
. This compiles, but you probably need to use some maps:
implicit val artifactModelReads: Reads[ArtifactModel] = (
(__ \ "_id").read[BSONObjectID] ~
(__ \ "name").read[String] ~
(__ \ "createdAt").read[Date] ~
(__ \ "updatedAt").read[Date] ~
(__ \ "attributes").read[List[AttributeModel]] ~
(__ \ "stateModels").read[List[StateModel]]
)(ArtifactModel.apply _)
您可以在阅读后像这样(CONVERT_TO_DATE
是虚构的):
You can after a read, like so (CONVERT_TO_DATE
is imaginary):
implicit val artifactModelReads: Reads[ArtifactModel] = (
(__ \ "_id").read[BSONObjectID] ~
(__ \ "name").read[String] ~
(__ \ "createdAt").read[String].map( s=>CONVERT_TO_DATE(s) ) ~
(__ \ "updatedAt").read[Date] ~
(__ \ "attributes").read[List[AttributeModel]] ~
(__ \ "stateModels").read[List[StateModel]]
)(ArtifactModel.apply _)
这篇关于为什么我会收到“应用程序不带参数"在Play Framework 2.3中使用JSON Read?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!