[英]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
"/customers/{id: .+}
<-- getCustomer "/customers/{id: .+}
<-- getCustomer"/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 表达式定义了严格的排序和优先级规则,并且基于最具体的匹配获胜算法。
{id}
or {id: .+}
.{id}
或{id: .+}
。 This sort is in descending order.{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.