问题描述
我正在开发一个带有 play frame work 的 Scala 应用程序,我创建了一个过滤器来过滤来自外部服务器的每个请求,但是现在我被困在两天后如何按需运行过滤器,我有80 个 API,其中 30 个需要运行特定的过滤器,我如何在这样的请求中读取请求路由模板
I'm developing a scala application with play frame work, i have created a filter that filters every request coming from outside server,but now i'm stuck on how can i run a filter on demand since two days,i have 80 APIs 30 of them needs to run a specific filter, how can i read the request route template while the requests like this
GET /api/v1/:locale/:uuid core.controllers.MyClass.myAction1(locale: String)
GET /api/v1/:locale/:uuid/MyRoute core.controllers.MyClass.myAction2(locale: String)
GET /api/v1/:locale/:uuid/Foo core.controllers.MyClass.myAction3(locale: String)
GET /api/v1/:locale/orders/:orderId core.controllers.MyClass.myAction4(locale: String)
好吧,这些路由放在路由文件中,在过滤器中,如果路线具有 :uuid 变量或 :orderId 变量,我需要检查天气以运行其特定过滤器,因为它们的两个 ID,我都将它们作为 uuid所以我不能指望这个请求,我可以读一下路由模板吗?
well, those routes are placed in routes file,in filter i need to check weather if the route has :uuid variable or :orderId in order to run its specific filter, because both of their ids, i getting them as uuid so i couldn't expect the request, could i read the route template ?
推荐答案
您可以从 RequestHeader#attrs
获取一些路由信息:
You can access to some routing information from the RequestHeader#attrs
:
// in your filter
val handlerDef: Option[HandlerDef] = request.attrs.get(Router.Attrs.HandlerDef)
这篇关于如何按需运行过滤器 Scala Play 框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!