zoukankan      html  css  js  c++  java
  • 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.")
      }
  • 相关阅读:
    被学长教会的高斯消元法Gauss
    KMP字符串匹配算法翔解❤
    fkwの题目(祝松松生日快乐!)
    NOI-linux下VIM的个人常用配置
    从2017年暑假到现在手打的模板↑_↑
    【テンプレート】初级数据结构
    【テンプレート】高精
    DP(第二版)
    luogu P1029 最大公约数和最小公倍数问题
    贪心题整理
  • 原文地址:https://www.cnblogs.com/jHenry/p/4277094.html
Copyright © 2011-2022 走看看