【问题标题】:Get all active Routes / Paths in a running Play application获取正在运行的 Play 应用程序中的所有活动路线/路径
【发布时间】:2014-10-20 12:42:20
【问题描述】:

是否有一种标准方法可以在播放应用程序中获取所有可能的(当然不包括通配符)路由/路径?

我可以做到的

Play.current.routes.map( _.documentation.map(_._2))

这给了我所有可用的路线,但对我来说它看起来有点 hacky。

【问题讨论】:

  • 这是在开发模式下遇到 404 错误时用于显示所有路由的类似方式。
  • 这似乎是唯一的方法,至少目前是这样。
  • 这似乎是获取 Play 应用程序所有路由的正确方法。然而,官方文档没有提到任何这样的事情。
  • 值得注意的是,如果您出于客户端目的需要此功能,play.api.Routes.javascriptRouter 实用程序方法可能会有所帮助。

标签: scala playframework playframework-2.3


【解决方案1】:

只是为了详细说明,我所做的是(正是你所做的):

  val myroutes = Play.current.routes map (routes => routes.documentation) getOrElse (Nil)
  myroutes foreach { r =>
    println("%-10s %-50s %s".format(r._1, r._2, r._3))
  }

我也得到:

GET        /                                                  controllers.Application.index
GET        /apis                                              controllers.Application.apis
GET        /docs                                              controllers.Application.docs
GET        /sampleapi/status/$id<[^/]+>                       controllers.SampleAPI.status(id:String)
GET        /city/$id<[^/]+>                                   controllers.CityAPI.findById(id:Long)
GET        /city1/$id<[^/]+>                                  controllers.CityAPI.findById1(id:Long)
GET        /apps/authorize/                                   controllers.Apps.authorize
POST       /apps/send_auth/                                   controllers.Apps.send_auth
GET        /clients/                                          controllers.Clients.list
GET        /client/new                                        controllers.Clients.create
POST       /client/add                                        controllers.Clients.add
POST       /client/update                                     controllers.Clients.update
GET        /client/edit/$id<[^/]+>                            controllers.Clients.edit(id:String)
PUT        /client/                                           controllers.Clients.update
GET        /client/$id<[^/]+>                                 controllers.Clients.get(id:String)
DELETE     /client/$id<[^/]+>                                 controllers.Clients.delete(id:String)
GET        /login                                             controllers.Auth.login
POST       /authenticate                                      controllers.Auth.authenticate
GET        /logout                                            controllers.Auth.logout
POST       /oauth2/access_token                               controllers.OAuth2Controller.accessToken
GET        /webjars/$file<.+>                                 controllers.WebJarAssets.at(file:String)
GET        /assets/$file<.+>                                  controllers.Assets.at(path:String = "/public", file:String)

这还包括 webjar 和 assets 路径,因此您可能希望将它们过滤掉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多