URL 矩阵参数与查询参数

发布于 2024-08-17 12:50:44 字数 1014 浏览 3 评论 0原文

我想知道是否在我的 URL 中使用矩阵或查询参数。我发现对该主题的较旧的讨论并不令人满意。

带有查询参数的示例

乍一看矩阵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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贩梦商人 2024-08-24 12:50:44

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

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

它实际上取决于命名空间。

注意:这里资源的“级别”是类别对象

如果仅将查询参数用于多级 URL,那么您最终会得到

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

这样的结果:这样您还会失去请求中参数的位置所带来的清晰度。此外,当使用像 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:

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.

诗酒趁年少 2024-08-24 12:50:44

除了 Tim Sylvester 的回答之外,我还想提供一个如何使用 JAX-RS

  1. 最后一个资源元素的矩阵参数

    http://localhost:8080/res/categories/objects;name=green
    

    您可以使用 @ 访问它们MatrixParam注释

    <前><代码>@GET
    @Path("类别/对象")
    公共字符串对象(@MatrixParam(“名称”)字符串对象名称){
    返回对象名;
    }

    回复

    <前><代码>绿色

    但就像 Javadoc 所说

    <块引用>

    请注意,@MatrixParam 注释值是指驻留在注入路径注释的 Java 结构的最后匹配路径段中的矩阵参数的名称。矩阵参数的值。

    ...是什么让我们来到第 2 点

  2. ...是什么让我们在 URL 中间

    矩阵参数

    http://localhost:8080/res/categories;name=foo/objects;name=green
    

    您可以使用路径变量和@PathParam在任何地方访问矩阵参数PathSegment

    <前><代码>@GET
    @Path("{categoryVar:类别}/对象")
    公共字符串objectsByCategory(@PathParam(“categoryVar”)PathSegment类别Segment,
    @MatrixParam("name") String objectName) {
    MultivaluedMap;矩阵参数=categorySegment.getMatrixParameters();
    StringcategorySegmentPath=categorySegment.getPath();
    String string = String.format("对象%s,路径:%s,matrixParams:%s%n", objectName,
    类别SegmentPath、矩阵参数);
    返回字符串;
    }

    回复

    对象绿色,路径:类别,matrixParams:[name = foo]
    

    由于矩阵参数作为 提供MultivaluedMap 您可以通过以下方式访问每个值

    列表<字符串>名称=matrixParameters.get(“名称”);
    

    或者如果您只需要第一个

    字符串名称=matrixParameters.getFirst(“名称”);
    

  3. 以一种方法获取所有矩阵参数参数

    http://localhost:8080/res/categories;name=foo/objects;name=green//attributes;name=size
    

    使用List获取全部

    <前><代码>@GET
    @Path("全部/{var:.+}")
    公共字符串 allSegments(@PathParam("var") ListpathSegments) {
    StringBuilder sb = new StringBuilder();

    for (PathSegment pathSegment : pathSegments) {
    sb.append("路径:");
    sb.append(pathSegment.getPath());
    sb.append(", 矩阵参数");
    sb.append(pathSegment.getMatrixParameters());
    sb.append(“
    ”);
    }

    返回 sb.toString();
    }

    回复

    路径:类别、矩阵参数 [name=foo]
    路径:对象、矩阵参数 [name=green]
    路径:属性、矩阵参数[name=size]
    

In addition to Tim Sylvester's answer I would like to provide an example of how matrix parameters can be handled with JAX-RS .

  1. Matrix parameters at the last resource element

    http://localhost:8080/res/categories/objects;name=green
    

    You can access them using the @MatrixParam annotation

    @GET
    @Path("categories/objects")
    public String objects(@MatrixParam("name") String objectName) {
      return objectName;
    }
    

    Response

    green
    

    But like the Javadoc states

    Note that the @MatrixParam annotation value refers to a name of a matrix parameter that resides in the last matched path segment of the Path-annotated Java structure that injects the value of the matrix parameter.

    ... what brings us to point 2

  2. Matrix parameters in the middle of an URL

    http://localhost:8080/res/categories;name=foo/objects;name=green
    

    You can access matrix parameters anywhere using path variables and @PathParam PathSegment.

    @GET
    @Path("{categoryVar:categories}/objects")
    public String objectsByCategory(@PathParam("categoryVar") PathSegment categorySegment, 
                                    @MatrixParam("name") String objectName) {
      MultivaluedMap<String, String> matrixParameters = categorySegment.getMatrixParameters();
      String categorySegmentPath = categorySegment.getPath();
      String string = String.format("object %s, path:%s, matrixParams:%s%n", objectName,
              categorySegmentPath, matrixParameters);
      return string;
    }
    

    Response

    object green, path:categories, matrixParams:[name=foo]
    

    Since the matrix parameters are provided as a MultivaluedMap you can access each by

    List<String> names = matrixParameters.get("name");
    

    or if you only need the first one

    String name = matrixParameters.getFirst("name");
    
  3. Get all matrix parameters as one method parameter

    http://localhost:8080/res/categories;name=foo/objects;name=green//attributes;name=size
    

    Use a List<PathSegment> to get them all

    @GET
    @Path("all/{var:.+}")
    public String allSegments(@PathParam("var") List<PathSegment> pathSegments) {
      StringBuilder sb =  new StringBuilder();
    
      for (PathSegment pathSegment : pathSegments) {
        sb.append("path: ");
        sb.append(pathSegment.getPath());
        sb.append(", matrix parameters ");
        sb.append(pathSegment.getMatrixParameters());
        sb.append("<br/>");
      }
    
      return sb.toString();
    }
    

    Response

    path: categories, matrix parameters [name=foo]
    path: objects, matrix parameters [name=green]
    path: attributes, matrix parameters [name=size]
    
守望孤独 2024-08-24 12:50:44

--太重要了,不能放在评论部分。--

我不确定矩阵 URL 有什么大不了的。根据 TBL 撰写的 w3c 设计文章,这只是一个设计想法,并明确指出它不是 Web 的功能。使用它时,诸如相对 URL 之类的东西不会被实现。如果你想用它,那没问题;只是没有标准的使用方法,因为它不是标准。

史蒂夫·波默罗伊

简而言之,如果您出于商业目的需要 RS,那么最好使用请求参数。

--Too important to be relegated to comment section.--

I'm not sure what the big deal is with matrix URLs. According to the w3c design article that TBL wrote, it was just a design idea and explicitly states that it's not a feature of the web. Things like relative URLs aren't implemented when using it. If you want to use it, that's fine; there's just no standard way to use it because it's not a standard.

Steve Pomeroy.

So short answer is, if you need RS for business purpose, you are better off using request parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文