URL矩阵参数与查询参数

URL矩阵参数与查询参数

本文介绍了URL矩阵参数与查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是在URL中使用矩阵还是查询参数.我发现对于该主题不满意的讨论. /p>

示例

乍看之下,矩阵参数似乎只有优点:

  • 更具可读性
  • 不对&"进行编码和解码XML文档中是必需的
  • 带有?"的URL在许多情况下不会被缓存;具有矩阵参数的URL被缓存
  • 矩阵参数可以出现在路径中的任意位置,并且不仅限于其结尾
  • 矩阵参数可以具有多个值:paramA=val1,val2

但是也有缺点:

  • 只有 JAX-RS 这样的少数框架支持矩阵参数
  • 当浏览器通过GET提交表单时,这些参数成为查询参数.因此,对于同一任务,它以两种参数结束.为避免混淆REST服务的用户并限制服务开发人员的工作量,在此区域中使用始终查询参数会更容易.

由于该服务的开发人员可以选择一个支持矩阵参数的框架,因此唯一的缺点是浏览器默认会创建查询参数.

还有其他缺点吗?你会怎么做?

解决方案

重要的区别在于,矩阵参数适用于特定的路径元素,而查询参数适用于整个请求.在对多个级别的资源和子资源进行复杂的REST样式查询时,这会起作用:

http://example.com/res/categories;name=foo/objects;name=green/?page=1

这实际上归结为命名空间.

注意:这里的资源级别"是categoriesobjects.

如果仅查询参数用于多级URL,您将最终获得

http://example.com/res?categories_name=foo&objects_name=green&page=1

这样,您还将失去请求中参数的局部性所增加的清晰度.此外,当使用JAX-RS之类的框架时,所有查询参数都将显示在每个资源处理程序中,从而导致潜在的冲突和混乱.

如果您的查询只有一个级别",那么区别并不是很重要,并且两种类型的参数可以有效地互换,但是,查询参数通常得到更好的支持并得到更广泛的认可.通常,我建议您坚持使用诸如HTML表单和简单的单级HTTP API之类的查询参数.

I'm wondering whether to use matrix or query parameters in my URLs. I found an older discussion to that topic not satisfying.

Examples

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?

解决方案

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:

http://example.com/res/categories;name=foo/objects;name=green/?page=1

It really comes down to namespacing.

Note: The 'levels' of resources here are categories and objects.

If only query parameters were used for a multi-level URL, you would end up with

http://example.com/res?categories_name=foo&objects_name=green&page=1

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.

这篇关于URL矩阵参数与查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:46