Jax-RS重载方法/路径执行顺序 [英] Jax-RS overloading methods/paths order of execution

查看:78
本文介绍了Jax-RS重载方法/路径执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序编写API,并且对于Jax-RS如何处理某些情况感到困惑

I am writing an API for my app, and I am confused about how Jax-RS deals with certain scenarios

例如,我定义了两个路径:

For example, I define two paths:

@Path("user/{name : [a-zA-Z]+}")

@Path("user/me")

我指定的第一个路径明确包含第二个路径,因为正则表达式包含所有字母a-z.但是,该程序似乎对此没有问题.是因为它默认为最特定的路径(即/me然后查找正则表达式)?

The first path that I specified clearly encompasses the second path since the regular expression includes all letters a-z. However, the program doesn't seem to have an issue with this. Is it because it defaults to the most specific path (i.e. /me and then looks for the regular expression)?

此外,如果我将两个正则表达式定义为具有某些重叠的路径,会发生什么情况.是否有默认方法会被调用?

Furthermore, what happens if I define two regular expressions as the path with some overlap. Is there a default method which will be called?

说我想为三种不同的方法创建三个路径:

Say I want to create three paths for three different methods:

@Path{"user/{name : [a-zA-Z]+}")
@Path("user/{id : \\d+}")
@Path("user/me")

这是最佳做法吗?它怎么知道要调用哪种方法?

Is this best practice/appropriate? How will it know which method to call?

谢谢您的澄清.

推荐答案

这位于

使用(1) E 进行排序,每个成员的文字字符数作为主键(降序),(2)捕获组的数量作为辅助键(降序),(3)具有非默认正则表达式(即,不是'([[^/] +?)')的捕获组的数量为三级键(降序),...

Sort E using (1) the number of literal characters in each member as the primary key (descending order), (2) the number of capturing groups as a secondary key (descending order), (3) the number of capturing groups with non-default regular expressions (i.e. not ‘([^ /]+?)’) as the tertiary key (descending order), ...

会发生什么,候选方法按指定的有序键"排序.我用粗体突出显示它们.

What happens is the candidate methods are sorted by specified ordered "key". I highlight them in bold.

第一个排序键是文字字符的数量.所以对于这三个

The first sort key is the number of literal characters. So for these three

@Path{"user/{name : [a-zA-Z]+}")
@Path("user/{id : \\d+}")
@Path("user/me") 

如果请求的URI是 ../user/me ,则将始终选择最后一个,因为它具有最多的文字字符(7,/个计数).其他人只有5.

if the requested URI is ../user/me, the last one will always be chosen, as it has the most literal characters (7, / counts). The others only have 5.

除了 ../users/me 之外,其他任何 ../users/..都将取决于正则表达式.在您的情况下,一个仅匹配数字,一个仅匹配字母.这两个正则表达式无法重叠.因此它将相应地匹配.

Aside from ../users/me anything else ../users/.. will depend on the regex. In your case one matches only numbers and one matches only letters. There is no way for these two regexes to overlap. So it will match accordingly.

现在只是为了好玩,假设我们有

Now just for fun, let's say we have

@Path{"user/{name : .*}")
@Path("user/{id : \\d+}")
@Path("user/me")

如果您看一下前两个,我们现在有重叠的正则表达式.第一个将匹配所有数字,第二个将匹配所有数字.那么将使用哪一个呢?我们不能做任何假设.这是一个不确定的级别,我从不同的实现中看到了不同的行为.AFAIK,没有最佳匹配"正则表达式的概念.要么匹配,要么不匹配.

If you look at the top two, we now have overlapping regexes. The first will match all numbers, as will the second one. So which one will be used? We can't make any assumptions. This is a level of ambiguity not specified and I've seen different behavior from different implementations. AFAIK, there is no concept of a "best matching" regex. Either it matches or it doesn't.

但是,如果我们希望始终先检查 {id:\\ d +} ,该怎么办.如果与数字匹配,则应选择该数字.我们可以根据规范对其进行修改.规范讨论的是捕获组",它们基本上是 {..} s.第二个排序键是捕获组的数量.我们破解的方法是添加另一个可选"组

But what if we wanted the {id : \\d+} to always be checked first. If it matches numbers then that should be selected. We can hack it based on the specification. The spec talks about "capturing groups" which are basically the {..}s. The second sorting key is the number of capturing groups. The way we could hack it is to add another "optional" group

@Path{"user/{name : .*}")
@Path("user/{id : \\d+}{dummy: (/)?}")

现在后者拥有更多的捕获群体,因此它将始终排在前列.它所做的只是允许一个可选的/,它实际上不会影响API,但是可以确保如果请求URI为全数字,则始终选择此路径.

Now the latter has more capturing groups so it will always be ahead in the sort. All it does is allow an optional /, which doesn't really affect the API, but insures that if the request URI is all numbers, this path will always be chose.

您可以在此答案中看到一些测试用例的讨论

这篇关于Jax-RS重载方法/路径执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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