在models
包中,我具有以下案例类:
case class Property (id: Option[Long],
address: String,
postCode: Int,
latitude: Double,
longitude: Double,
surface: Option[Int],
bedRoomCount: Option[Int])
object Property {
implicit val propertyFormat = Json.format[Property]
}
我正在尝试配置传递Property对象的路由:
POST /update-property controllers.PropertyController.update(property: models.Property)
我的控制器定义了一个动作:
def update(property: Property) = Action.async { implicit request =>
...bla bla bla...
}
我收到以下编译错误:
[error] conf/routes:8:1: No QueryString binder found for type models.Property. Try to implement an implicit QueryStringBindable for this type.
[error] POST /update-property controllers.PropertyController.update(property: models.Property)
[error] conf/routes:8:1: not enough arguments for method implicitly: (implicit e: play.api.mvc.QueryStringBindable[models.Property])play.api.mvc.QueryStringBindable[models.Property].
[error] Unspecified value parameter e.
我想念什么?可以用property
数据填充表单吗?
答案 0 :(得分:1)
就像编译错误指出的那样,您需要实现implicit QueryStringBindable
。像这样:
object Binders {
//return the error message on the left should the parsing fail
private def propertyFromString(s: String): Either[String, Property] = ???
private def propertyToString(property: Property): String = ???
implicit def queryStringBindable(implicit stringBinder: QueryStringBindable[String]): QueryStringBindable[Property] = new QueryStringBindable[Property] {
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, Property]] = {
for {
eitherPropString <- stringBinder.bind("property", params)
} yield {
eitherPropString match {
case Right(propString) => propertyFromString(propString)
case _ => Left("Unable to bind property")
}
}
}
override def unbind(key: String, property: Property): String = {
stringBinder.unbind("property", propertyToString(property))
}
}
}
对于???
,您将需要编写代码以将Property
与String
进行编码/解码。然后,在您的build.sbt
文件中,添加
routesImport += "path.to.Binders._"
,以便您的routes
文件可以访问您的Binders
对象。该文档为here。相反,如果您想将Property
字段作为单独的查询参数传递,请参阅文档中的AgeRange
示例。