[英]Spray - deserializing optional query parameter
From spray.io documentation page: 从spray.io文档页面:
color
extract value of parameter “color” as Stringcolor
提取参数“ color”的值作为String
color.?
extract optional value of parameter “color” as Option[String]提取参数“ color”的可选值作为Option [String]
amount.as[Int]
extract value of parameter “amount” as Int, you need a matching Deserializer in scope for that to work (see also Unmarshalling)amount.as[Int]
将参数“ amount”的值提取为Int,在作用域中需要一个匹配的反序列化器才能起作用(另请参见解组)
So how can I parse optional Int
value? 那么如何解析可选的
Int
值? Something like amount.?.as[Int]
doesn't seem to work. 诸如
amount.?.as[Int]
类的东西似乎不起作用。
You can see the details here: https://github.com/spray/spray/blob/76ab89c25ce6d4ff2c4b286efcc92ee02ced6eff/spray-routing/src/main/scala/spray/routing/directives/NameReceptacle.scala 您可以在此处查看详细信息: https : //github.com/spray/spray/blob/76ab89c25ce6d4ff2c4b286efcc92ee02ced6eff/spray-routing/src/main/scala/spray/routing/directives/NameReceptacle.scala
case class NameReceptacle[A](name: String) {
def as[B] = NameReceptacle[B](name)
def as[B](deserializer: FSOD[B]) = NameDeserializerReceptacle(name, deserializer)
def ? = as[Option[A]]
def ?[B](default: B) = NameDefaultReceptacle(name, default)
def ![B](requiredValue: B) = RequiredValueReceptacle(name, requiredValue)
}
The straightforward syntax would be 简单的语法是
"amount".as[Option[Int]]
Unfortunately there is no syntactic sugar to create a NameRecaptable
for an option type directly, but you can do it in two steps: 不幸的是,没有语法糖可以直接为选项类型创建
NameRecaptable
,但是您可以分两个步骤进行操作:
"amount".as[Int].as[Option[Int]]
?
is an alias for NameRecaptable[A].as[Option[A]]
, so you can use the following code (note the postfix operator syntax): 是
NameRecaptable[A].as[Option[A]]
的别名,因此您可以使用以下代码(请注意后缀运算符语法):
"amount".as[Int]?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.