本文介绍了在 REST Web 服务中使用查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为使用 REST Web 服务的一个主要特征和原因是使用路径参数而不是查询参数.但是许多公开可用的 REST Web 服务都使用查询参数.

I thought one main characteristic and reason for using REST web services was to use path parameters rather than query parameters. But many publicly available REST web services use query parameters.

我认为查询参数不应该用于 REST Web 服务是错误的吗?是否有关于不在 REST Web 服务中使用查询参数的建议或规则?

Am I wrong in thinking that query parameters are not supposed to be used in REST web services?Is there a recommendation or rule about not using query parameters in REST web services?

推荐答案

查询字符串仍然可以在 REST Web 服务中使用,只是方式不同.

The query string can still be used in REST web services just not in the same way as normal.

您必须将 URL 视为资源的.URL 是资源的唯一标识符.例如

You have to think of the URL as the key to a resource. The URL is an unique identifier for the resource. For example

http://example.com/products/123   -- where 123 is the id of the products.

访问 /products 将返回完整的产品列表.添加 ID 将返回特定产品.

Accessing to /products would return a full list of products. Adding the id would return a specific product.

如果您想以特定方式订购产品怎么办?有人会说

What if you want to order product in a specific way? Some would say

http://example.com/products/united-states

好吧,现在乍一看有些歧义.美国是身份证吗?那么这个歧义可以通过说一个 id 表示为 \d+ 来解决.正确.

Well, now there is some ambiguity at the first look. Is united-states an id? Well that ambiguity can be solved by saying an id is represented as \d+. Correct.

好的,所以我们的第一个由单词组成的参数是一个国家.

Ok so our first parameters which is made of words is a country.

现在假设我们想要添加更多过滤器,让我们尝试添加更多斜杠.

Now lets say we want to add more filters, lets try and add more slashes.

http://example.com/products/united-states/home/asc

但我不想要美国产品!但还是想要家居用品.

But I don't want united states products only! But still want home products.

http://example.com/products/home/asc

等等...家是一个国家吗?我现在不确定,这有点模棱两可......如果我明天想添加另一个过滤器怎么办?我会怎么做...添加更多斜线?

Wait... is home a country? I'm not sure now, it's kind of ambiguous...And what if I want to add another filter tomorrow? What will I do... add more slashes?

URL 变得杂乱无章,充满了模糊的参数,这些参数起初是可选的,但由于歧义而变成必需的.

The URL becomes cluttered and full of ambiguous parameters that were optional at first and that become obligatory because of ambiguity.

对我来说,正确的方法是将 查询字符串 用于特定于查询的内容.因为,我可以按我想要的任何方式对查询进行排序,它仍然是相同的查询.我查询产品.

The correct way, to me, would be to use the query string for thing specific about the query.Because, I can sort the query in any way I want, it is still the same query. I query products.

所以表格应该是这样的

http://example.com/products -- all products
http://example.com/products/{id} -- specific one
http://example.com/products/?country=united-sites -- filtered

通过这种方式,您可以随时添加新的过滤器,并保持清晰的 URL,即使您更改过滤器也不会中断.

This way you can add new filters anytime you want and keep URLs that are clear and won't ever break even though you change the filters.

如果您想了解更多信息,我真的建议您查看本次会议 由 David Zülke 主持,他是为 Symfony 框架工作的人.他谈到了很多 REST 网络服务,但他也专门谈到了 URL,以及如何构建它们(主要是 16 到 30 分钟).

If you want more information I really, really advise you to look at this conference by David Zülke, a guy working for the Symfony framework. He talks about a lot of things of REST web-services, but he also talk specifically about URLs, and how to build them (Mainly from 16 to 30 minutes).

您还可以查看 apigee 网站.他们有很多关于 REST 的视频(和书籍).更具体地说,这个视频,这是真的主题在这里.

You can also look at apigee website. They have a lot of videos (and books) about REST. More specifically this video, which is really on topic here.

这篇关于在 REST Web 服务中使用查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 23:07
查看更多