JAX-RS 中如何控制调度?
我正在使用 RESTeasy 编写 RESTful Web 服务。这是 JAX-RS 的实现。您可以使用单个 @Path 注释来注释类或方法。正则表达式用于获取路径参数。例如,
@Path("/foo{varname:.*}/bar")
匹配以“/foo”开头、以“/bar”结尾以及其间的任何内容的所有模式。两者之间的任何内容都会分配给名为 varname 的参数。
一些框架(如 Django)有一个正则表达式和方法列表,将按顺序尝试。例如/john/q/smith、/john/{.*}/smith、/john/{.*}/{.*}。 “/john/henry/smith”匹配第二个和第三个,但第二个将被调度,因为它是找到的第一个匹配项。
这在 JAX-RS 中可能吗?或者类和方法没有固有的顺序吗?对于 /john/{.*}/{.*} 您是否必须编写一个表示 /john/anything/anythingbutsmith 的正则表达式?每次更改其他项时,您都必须更改它。
I'm writing a RESTful Web Service with RESTeasy. This is an implementation of JAX-RS. You annotate a class or method with a single @Path annotation. Regular expressions are used to get path parameters. For instance
@Path("/foo{varname:.*}/bar")
matches all patterns starting with "/foo", ending with "/bar" and having anything in between. Whatever is in between is assigned to a parameter named varname.
Some frameworks (like Django) have a list of regular expressions and methods that will be tried in order. For instance /john/q/smith, /john/{.*}/smith, /john/{.*}/{.*}. "/john/henry/smith" matches the second and third, but the second one will be dispatched because it is the first match found.
Is this possible in JAX-RS, or is there no inherent order to the classes and methods? For /john/{.*}/{.*} would you have to write a regex that means /john/anything/anythingbutsmith? You would have to change it every time you changed the other ones.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个定义明确的算法,JAX-RS 规范的第 3.7.1 节对此进行了描述。坦率地说,我发现这个解释相当不透明 - 所以读完它后,我无法回答你的问题。
但是,我刚刚找到了 CXF 概述 选择算法,这似乎表明优先规则确实可以让您做您想做的事。
There is a well defined algorithm, section 3.7.1 of the JAX-RS spec describes it. Frankly, I find the explanation pretty opaque - so reading it, I can't answer your question.
However, I've just found the CXF overview of the selection alogorithm, and that seems to indicate that the precedence rules do indeed let you do what you want.
优先规则
JAX-RS
规范定义了严格的排序以及匹配 URI 表达式的优先规则,并且基于最具体的
匹配获胜算法。
JAX-RS
提供程序收集已部署的 URI 表达式集并根据以下逻辑对它们进行排序:
排序的主键是完整 URI 中的文字字符数
匹配模式。排序是按降序排列的。在我们的模棱两可的例子中,
getCustomer() 的模式有 11 个文字字符:/customers/。获取广告
dress() 方法的模式有 18 个文字字符:/customers/ 加上地址。
因此,JAX-RS 提供者将在之前尝试匹配 getAddress() 的模式
getCustomer()。
排序的辅助键是嵌入的模板表达式的数量
在模式内,即 {id} 或 {id : .+}。这种排序是按降序排列的。
3. 排序的第三键是非默认模板表达式的数量。一个
默认模板表达式是不定义正则表达式的模板表达式,即
是,{id}。
Precedence rules
The
JAX-RS
specification has defined strict sortingand precedence rules for matching URI expressions and is based on a most specific
match wins algorithm. The
JAX-RS
provider gathers up the set of deployed URI expressionsand sorts them based on the following logic:
The primary key of the sort is the number of literal characters in the full URI
matching pattern. The sort is in descending order. In our ambiguous example,
getCustomer()’s pattern has 11 literal characters: /customers/. The getAd
dress() method’s pattern has 18 literal characters: /customers/ plus address.
Therefore, the JAX-RS provider will try to match getAddress()’s pattern before
getCustomer().
The secondary key of the sort is the number of template expressions embedded
within the pattern—that is, {id} or {id : .+}. This sort is in descending order.
3. The tertiary key of the sort is the number of nondefault template expressions. A
default template expression is one that does not define a regular expression—that
is, {id}.