java - REST 路径参数与资源路径冲突?
问题描述
这个问题是在我使用 JAX-RS 构建后端时出现的,但这确实适用于任何 REST API。
JAX-RS 如何处理可能由于参数路径变量而发生冲突的路径?假设你有
@POST
@Path('createBox/{boxName}')
foo()
@POST
@Path('createBox/small')
bar()
有人想用路径参数参数调用第一个端点small
。在这种情况下会发生什么?如果foo()
和bar()
有不同的帕尔马(也许像@FormParam
),那会有助于区分吗?如果它们完全一样,没有参数怎么办?行为是不确定的吗?
解决方案
Reference : RESTful Java with JAX-RS 2.0, 2nd Edition by Bill Burke
"/customers/{id : .+}
<-- getCustomer"/customers/{id : .+}/address"
<-- getAddress
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.
- 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)
- 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. - 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}
Your example:
bar()
wins because as per rule 1, it has more literal characters
推荐阅读
- c# - 是否可以使用 c# 在屏幕上的任意位置移动光标?
- javascript - 如何使用 jquery 获取列表元素?
- database - 手动同步数据库操作并避免内置数据库同步机制是否明智?
- html - Adobe XD HTML 文件导出视图大小对于浏览器来说太大了。我怎样才能让它变小?
- ios - swiftui 数组内容在函数闭包内更新,但不保留在其他视图中
- php - 如何计算数据库中的行数
- kubernetes - 在 Kops v 1.16 中,尽管集群运行良好,但在 aws 点实例组的“kops rolling-update cluster”中获取状态 NeedsUpdate 和 Ready 0
- django - 单元测试:如何模拟 MEDIA_ROOT 但仍访问存储在我的普通 MEDIA 文件夹中的文件
- opencl - OpenCL 2.0 设备命令队列不断填满并停止执行
- reactjs - 按下反应钩子时显示工具提示并在几秒钟后隐藏