如何使用可选参数设计用于搜索的 RESTful URL? [英] How to design a RESTful URL for search with optional parameters?

查看:65
本文介绍了如何使用可选参数设计用于搜索的 RESTful URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我必须在我的 RESTful Web 服务中创建一个 URL,我的客户将使用它来按字段是可选的字段搜索所有企业,该 URL 会是什么样子?

If I have to create a URL in my RESTful web service, which would be used by my clients to search all businesses by their fields where the fields are optional, what would the URL look like?

企业可以仅按名称、姓名和电话号码或姓名、电话号码和联系电子邮件进行搜索.

A business can be search by their name alone, name and phone number or name, phone number and contact e-mail.

推荐答案

Chandru,想想所有业务的列表,就像一组具有属性的实体.您可以创建 URI,通过使用 URI 中的参数来标识(选择)一个子集.

Chandru, think of the list of all the businesses like of a set of entities with attributes. You can create URIs that identify (select) a subset by the use of parameters in the URI.

通常这是通过查询字符串参数('?' 后面的内容)完成的,但您也可以将参数指定为路径段或矩阵 URI.

Commonly this is done by query string parameters (the stuff after the '?') but you can also specify parameters as path segments or matrix URIs.

最典型的方法是这样的

  • http://foo.org/service/businesses/?name=acme or
  • http://foo.org/service/businesses/?name=acme&phone=12345 or
  • http://foo.org/service/businesses/?name=acme&contact=smith@bar.org ('@' would need URL encoding of course)

它在概念上类似于 SQL select 子句.

It is conceptually similar to an SQL select clause.

路径段中的参数或矩阵参数对索引可能性有影响(例如,矩阵参数允许您在多个级别进行过滤,因为层次结构可以在病房之后继续,而查询参数则不能).如果您对此感到担忧,我建议您提出一个不同的问题.

Parameters in path segments or matrix parameters have an impact regarding the indexing possibilities (e.g. matrix parameters allow you to filter at multiple levels because the hierarchy can continue after wards, which it cannot with query parameters). I suggest you make that a different question if you are concerned with it.

示例:

http://foo.org/service/businesses/france/name=acme;city=paris/latest/?contact=xxx

一月

这篇关于如何使用可选参数设计用于搜索的 RESTful URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆