简体   繁体   English

REST 路径参数与资源路径冲突?

[英]REST Path params conflicting with resource path?

This question came up while I was building a backend using JAX-RS but this can really apply to any REST API.这个问题是在我使用 JAX-RS 构建后端时出现的,但这确实适用于任何 REST API。

How does JAX-RS handle paths that might conflict due to param path variables? JAX-RS 如何处理可能由于参数路径变量而发生冲突的路径? Suppose you have假设你有

@POST
@Path('createBox/{boxName}')
foo()

@POST
@Path('createBox/small')
bar()

And someone wants to call the first endpoint with a path param argument of small .有人想用路径参数调用第一个端点small What happens in this case?在这种情况下会发生什么? If foo() and bar() had different parmas (maybe like @FormParam ) would that help differentiate?如果foo()bar()具有不同的参数(可能像@FormParam ),这有助于区分吗? What if they were exactly the same with no arguments?如果它们在没有 arguments 的情况下完全相同怎么办? Is the behavior non-deterministic?行为是不确定的吗?

Reference: RESTful Java with JAX-RS 2.0, 2nd Edition by Bill Burke参考: RESTful Java with JAX-RS 2.0, 2nd Edition by Bill Burke

  1. "/customers/{id: .+} <-- getCustomer "/customers/{id: .+} <-- getCustomer
  2. "/customers/{id: .+}/address" <-- getAddress "/customers/{id: .+}/address" <-- 获取地址

Precedence rules优先规则

The JAX-RS specification has defined strict sorting and precedence rules for matching URI expressions and is based on a most specific match wins algorithm. JAX-RS 规范为匹配 URI 表达式定义了严格的排序和优先级规则,并且基于最具体的匹配获胜算法。

  1. The primary key of the sort is the number of literal characters in the full URI matching pattern and is in descending order ( 11 in getCustomer vs 18 in getAddress)排序的主键是完整 URI 匹配模式中的文字字符数,并且按降序排列(getCustomer 中为 11,getAddress 中为 18)
  2. The secondary key of the sort is the number of template expressions embedded within the pattern—that is, {id} or {id: .+} .排序的辅助键是嵌入在模式中的模板表达式的数量——即{id}{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}默认模板表达式是一个没有定义正则表达式的模板,即{id}

Your example:你的例子:

bar() wins because as per rule 1, it has more literal characters bar()获胜,因为根据规则 1,它具有更多文字字符

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

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