【问题标题】:Scala PLAY same routesScala PLAY 相同的路线
【发布时间】:2017-01-13 04:00:41
【问题描述】:

我在路由文件中有相同的路由,但它们的操作不同,如图所示

GET         /counts                                 controllers.Application.getAllCountsByFeature(features)
GET         /counts                                 controllers.Application.getAllCounts()

我将两条路线都称为

http://localhost:9000/segments/counts?features=feature_1,feature_2-feature_3
http://localhost:9000/segments/counts

但它不起作用。我想根据查询字符串识别调用哪个路由。如果提供了查询字符串,那么它应该命中 getAllCountsByFeature 方法等等。

有什么办法吗? 我正在使用 Play 2.5.9

【问题讨论】:

  • 只用一个路由,在一个动作中做不同的查询
  • 我建议使用 2 条路由或 1 条路由,并使用可选查询参数在控制器内部执行逻辑,如前所述

标签: scala playframework routes playframework-2.0


【解决方案1】:

使用一个带有可选参数的路由

GET         /counts                                 controllers.Application.getAllCountsByFeature(features: Option[String])

然后

def getAllCountsByFeature(features: Option[String]) = Action {
  features match{
    case Some(f) => //..
    case None => getAllCounts()
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2017-08-29
    • 2016-10-12
    相关资源
    最近更新 更多