Play框架路由中的非捕获通配符 [英] Non-capturing wildcards in Play Framework routes

查看:59
本文介绍了Play框架路由中的非捕获通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Play公开一个HTTP API,并且为了管理破坏兼容性的更改,URL包含版本号.目前,它如下所示:

GET   /api/v1/someMethod       com.foo.Api.someMethod()

当我对其中一种方法的输出进行更改时,我想支持v2.尽管对于大多数方法而言,其行为是相同的,所以我不在乎使用哪个版本.我试图将上面的行修改为:

GET   /api/v:version/someMethod       com.foo.Api.someMethod()

但是Play无法编译它,错误为Missing parameter in call definition: version.

知道,我没有在通话中使用version参数-因为我不需要!是否有一种明智的方式来实现我的目标,要么让Play跳过此检查,要么在未捕获为参数的路由中放置通配符?

(我想如果不能的话,可以将参数添加到方法定义中,然后将其忽略.但是如果可能的话,我宁愿避免这样做.)

解决方案

尝试解决此问题已经有一段时间了,我怀疑这是不可能的.

最大的症结是反向路由. Play希望我能够在模板中使用@routes.com.foo.Api.someMethod,并将其解析为将调用该方法的URL. (实际上,我是在我的API文档中做到这一点的).如果我的上述建议中的任何一个都被接受,则与该方法相对应的实际URL是任意的.

我想我真正想要的是该方法具有规范的URL,但对于其他类似的模式也应视为匹配项.我接受Play并未将其作为相对简单的route文件语法的一部分提供,而我必须自己完成(例如,使用两种模式,最终使用通配符,但不直接调用相同的方法).

I'm exposing an HTTP API through Play, and in order to manage compatibility-breaking changes, the URL contains the version number. At present this looks like the following:

GET   /api/v1/someMethod       com.foo.Api.someMethod()

As I introduce a change to the output of one of the methods, I'd like to support v2. For most of the methods though, the behaviour is identical, so I don't care which version is used. I tried to modify the above line to:

GET   /api/v:version/someMethod       com.foo.Api.someMethod()

But Play fails to compile this, with the error Missing parameter in call definition: version.

I know I didn't use the version parameter in the call - because I didn't need to! Is there a sensible way to achieve what I'm after here, either to get Play to skip this check, or to put a wildcard in the route that is not captured as a parameter?

(I suppose if not I could add the parameter to the method definition, and then ignore it. But I'd rather avoid that if possible.)

解决方案

Having played around with this for a while trying to find workarounds, I suspect it may not be possible.

The big sticking point is reverse routing. Play wants it to be possible for me to be able to use @routes.com.foo.Api.someMethod in my templates, and have it resolved to the URL that would invoke that method. (And in fact I do this in my API docs). If either of my above proposals were to be accepted, it would be arbitrary what the actual URL was that corresponded to the method.

I suppose what I really want is for the method to have a canonical URL, but for other similar patterns to be considered a match too. I accept that Play does not offer this as part of the relatively simple routes file syntax, and I'd have to accomplish it myself (e.g. by using two patterns, with the wildcard one ultimately but not directly invoking the same method).

这篇关于Play框架路由中的非捕获通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆