【发布时间】:2015-01-07 10:11:00
【问题描述】:
我有基于 JSON 和播放框架的 scala REST 服务。我有用户案例类
case class User(_id: BSONObjectID, username: String,password: String,creationTime: org.joda.time.DateTime)
和
object User{
val userReads: Reads[User] = (
(JsPath \ "username").read[String] (minLength[String](4) keepAnd maxLength[String] (32) ) and
(JsPath \ "password").read[String] (minLength[String](8) keepAnd maxLength[String] (32) )
?????)(User.apply _)
val userWrites: Writes[User] = ....
implicit val userFormat: Format[User] = Format (userReads, userWrites)
}
在注册期间(通过 REST API),我需要验证传入的 json。我只需要用户名和密码,不需要 _id、creationTime 等。如何正确编写读取、写入以仅验证字段的子集(请在您的代码上完成读取并替换“?????”)?
【问题讨论】:
-
因为它们不是选项,我认为您需要提供这些值来创建现有类的实例。在任何其他时间点,您的前端是否需要创建时间/mongo id?
标签: json scala rest playframework