spray parameter directives

URL上的QueryString,使用paramters directive解析

必传参数(Required parameter)

http://localhost/api?color=blue&backgroundColor=orange
val route = parameters('color, 'backgroundColor) { (color, backgroundColor) => complete(s"The color is '$color' and the background is '$backgroundColor'") }

可选参数(Optional parameter)

http://localhost/api?color=blue
val route = parameters('color, 'backgroundColor.?) { (color, backgroundColor) => val backgroundStr = backgroundColor.getOrElse("<undefined>") complete(s"The color is '$color' and the background is '$backgroundStr'") }

设置可选参数的默认值(Optional parameter with default value)

http://localhost/api?color=blue等同于http://localhost/api?color=blue&backgroundColor=white
val route =
  parameters('color, 'backgroundColor ? "white") { (color, backgroundColor) =>
    complete(s"The color is '$color' and the background is '$backgroundColor'")
  }
 
 

限制参数的值(Parameter with required value)

Ok       http://localhost/api?color=blue&action=true
NotFound http://localhost/api?color=blue&action=false
val route = parameters('color, 'action ! "true") { (color) => complete(s"The color is '$color'.") }
//note: require value of parameter “color” to be "blue" and extract nothing

反序列化,将字符串转换为强类型(Deserialized parameter)

Ok         http://localhsot/api?color=blue&count=42
BadRequest http://localhost/api?color=blue&count=blue

val route =
  parameters('color, 'count.as[Int]) { (color, count) =>
    complete(s"The color is '$color' and you have $count of it.")
  }

 

转载于:https://www.cnblogs.com/jHenry/p/4277094.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值