URL 矩阵参数与查询参数
我想知道是否在我的 URL 中使用矩阵或查询参数。我发现对该主题的较旧的讨论并不令人满意。
带有查询参数的示例
- URL: http://some.where/thing?paramA=1¶mB =6542
- 带有矩阵参数的 URL:http://some.where/thing; paramA=1;paramB=6542
乍一看矩阵params似乎只有优点:
- 可读性更强,
- 无需对“&”进行编码和解码XML 文档中的 URL 必须
- 带有“?”很多情况下不被缓存;带有矩阵参数的 URL 是缓存的
- 矩阵参数可以出现在路径中的任何地方并且不限于其末尾
- 矩阵参数可以有多个值:
paramA=val1,val2
但也有缺点:
- 只能有一个值很少有像 JAX-RS 这样的框架支持矩阵参数
- 当浏览器通过 GET 提交表单时,参数变成查询参数。因此,对于同一任务,最终会产生两种参数。为了不让 REST 服务的用户感到困惑并限制服务开发人员的工作量,在此区域中使用始终查询参数会更容易。
由于服务的开发人员可以选择支持矩阵参数的框架,因此唯一剩下的缺点是浏览器默认创建查询参数。
还有其他缺点吗?你会怎么办?
I'm wondering whether to use matrix or query parameters in my URLs. I found an older discussion to that topic not satisfying.
Examples
- URL with query params: http://some.where/thing?paramA=1¶mB=6542
- URL with matrix params: http://some.where/thing;paramA=1;paramB=6542
At first sight matrix params seem to have only advantages:
- more readable
- no encoding and decoding of "&" in XML documents is required
- URLs with "?" are not cached in many cases; URLs with matrix params are cached
- matrix parameters can appear everywhere in the path and are not limited to its end
- matrix parameters can have more than one value:
paramA=val1,val2
But there are also disadvantages:
- only a few frameworks like JAX-RS support matrix parameters
- When a browser submits a form via GET, the params become query params. So it ends up in two kinds of parameters for the same task. To not confuse users of the REST services and limit the effort for the developers of the services, it would be easier to use always query params - in this area.
Since the developer of the service can choose a framework with matrix param support, the only remaining disadvantage would be that browsers create by default query parameters.
Are there any other disadvantages? What would you do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重要的区别在于矩阵参数适用于特定路径元素,而查询参数适用于整个请求。当对多个级别的资源和子资源进行复杂的 REST 样式查询时,这一点就会发挥作用:
它实际上取决于命名空间。
注意:这里资源的“级别”是
类别
和对象
。如果仅将查询参数用于多级 URL,那么您最终会得到
这样的结果:这样您还会失去请求中参数的位置所带来的清晰度。此外,当使用像 JAX-RS 这样的框架时,所有查询参数都会显示在每个资源处理程序中,从而导致潜在的冲突和混乱。
如果您的查询只有一个“级别”,那么差异并不重要,并且两种类型的参数可以有效地互换,但是,查询参数通常得到更好的支持和更广泛的认可。一般来说,我建议您坚持使用 HTML 表单和简单的单级 HTTP API 等查询参数。
The important difference is that matrix parameters apply to a particular path element while query parameters apply to the request as a whole. This comes into play when making a complex REST-style query to multiple levels of resources and sub-resources:
It really comes down to namespacing.
Note: The 'levels' of resources here are
categories
andobjects
.If only query parameters were used for a multi-level URL, you would end up with
This way you would also lose the clarity added by the locality of the parameters within the request. In addition, when using a framework like JAX-RS, all the query parameters would show up within each resource handler, leading to potential conflicts and confusion.
If your query has only one "level", then the difference is not really important and the two types of parameters are effectively interchangeable, however, query parameters are generally better supported and more widely recognized. In general, I would recommend that you stick with query parameters for things like HTML forms and simple, single-level HTTP APIs.
除了 Tim Sylvester 的回答之外,我还想提供一个如何使用 JAX-RS 。
最后一个资源元素的矩阵参数
您可以使用
@ 访问它们MatrixParam
注释<前><代码>@GET
@Path("类别/对象")
公共字符串对象(@MatrixParam(“名称”)字符串对象名称){
返回对象名;
}
回复
<前><代码>绿色
但就像 Javadoc 所说
<块引用>
请注意,
@MatrixParam
注释值是指驻留在注入路径注释的 Java 结构的最后匹配路径段中的矩阵参数的名称。矩阵参数的值。...是什么让我们来到第 2 点
矩阵参数
您可以使用路径变量和@PathParam在任何地方访问矩阵参数
PathSegment
。<前><代码>@GET;矩阵参数=categorySegment.getMatrixParameters();
@Path("{categoryVar:类别}/对象")
公共字符串objectsByCategory(@PathParam(“categoryVar”)PathSegment类别Segment,
@MatrixParam("name") String objectName) {
MultivaluedMap
StringcategorySegmentPath=categorySegment.getPath();
String string = String.format("对象%s,路径:%s,matrixParams:%s%n", objectName,
类别SegmentPath、矩阵参数);
返回字符串;
}
回复
由于矩阵参数作为 提供
MultivaluedMap
您可以通过以下方式访问每个值或者如果您只需要第一个
以一种方法获取所有矩阵参数参数
使用
List
获取全部<前><代码>@GETpathSegments) {
@Path("全部/{var:.+}")
公共字符串 allSegments(@PathParam("var") List
StringBuilder sb = new StringBuilder();
for (PathSegment pathSegment : pathSegments) {
sb.append("路径:");
sb.append(pathSegment.getPath());
sb.append(", 矩阵参数");
sb.append(pathSegment.getMatrixParameters());
sb.append(“
”);
}
返回 sb.toString();
}
回复
In addition to Tim Sylvester's answer I would like to provide an example of how matrix parameters can be handled with JAX-RS .
Matrix parameters at the last resource element
You can access them using the
@MatrixParam
annotationResponse
But like the Javadoc states
... what brings us to point 2
Matrix parameters in the middle of an URL
You can access matrix parameters anywhere using path variables and
@PathParam
PathSegment
.Response
Since the matrix parameters are provided as a
MultivaluedMap
you can access each byor if you only need the first one
Get all matrix parameters as one method parameter
Use a
List<PathSegment>
to get them allResponse
--太重要了,不能放在评论部分。--
简而言之,如果您出于商业目的需要 RS,那么最好使用请求参数。
--Too important to be relegated to comment section.--
So short answer is, if you need RS for business purpose, you are better off using request parameter.