简体   繁体   English

Jax-RS重载方法/路径执行顺序

[英]Jax-RS overloading methods/paths order of execution

I am writing an API for my app, and I am confused about how Jax-RS deals with certain scenarios 我正在为我的应用程序编写API,我对Jax-RS如何处理某些场景感到困惑

For example, I define two paths: 例如,我定义了两个路径:

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

and

@Path("user/me")

The first path that I specified clearly encompasses the second path since the regular expression includes all letters az. 我指定的第一条路径明确包含第二条路径,因为正则表达式包括所有字母az。 However, the program doesn't seem to have an issue with this. 但是,该程序似乎没有问题。 Is it because it defaults to the most specific path (ie /me and then looks for the regular expression)? 是因为它默认为最具体的路径(即/ me然后查找正则表达式)?

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? 它将如何知道调用哪种方法?

Thank you in advance for any clarification. 提前感谢您的任何澄清。

This is in the spec in "Matching Requests to Resource Methods" 这是在“匹配资源方法请求”中的规范

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 (ie not '([^ /]+?)') as the tertiary key (descending order), ... 使用(1)每个成员中的文字字符数作为主键(降序), (2)捕获组的数量作为辅助键(降序), (3)捕获组的数量来排序E -default正则表达式(即不是'([^ /] +?)')作为三级键(降序),...

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") 

if the requested URI is ../user/me , the last one will always be chosen, as it has the most literal characters (7, / counts). 如果请求的URI是../user/me ,则将始终选择最后一个,因为它具有最多的文字字符(7, /计数)。 The others only have 5. 其他人只有5人。

Aside from ../users/me anything else ../users/.. will depend on the regex. 除了../users/me其他任何东西../users/..将取决于正则表达式。 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")

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. AFAIK,没有“最佳匹配”正则表达式的概念。 Either it matches or it doesn't. 它匹配或不匹配。

But what if we wanted the {id : \\\\d+} to always be checked first. 但是,如果我们希望始终首先检查{id : \\\\d+} ,该怎么办? 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. 该规范谈到了“捕获群体”,这些群体基本上是{..} 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: (/)?}")

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. 它只是允许一个可选的/ ,它不会真正影响API,但确保如果请求URI是所有数字,将始终选择此路径。

You can see a discussion with some test cases in this answer 您可以在此答案中看到与某些测试用例的讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM