简体   繁体   English

如何为 HTTP GET 的多个键值参数设计 REST URI

[英]How to design REST URI for multiple Key-Value params of HTTP GET

I am designing a RESTful API.我正在设计一个 RESTful API。

One service should offer query functionality for multiple key-value pairs.一项服务应该为多个键值对提供查询功能。 For example, the client can query with one HTTP GET request for different products and the associated quantity.例如,客户端可以使用一个 HTTP GET 请求查询不同的产品和相关数量。

The client wants to query product 1 with the amount 44 AND product 2 with the amount 55. I actually don't want my URI to look like this:客户想查询金额为 44 的产品 1 和金额为 55 的产品 2。我实际上不希望我的 URI 看起来像这样:

/produkt?productId1=1&productquantity1=44&productId2=2&productquantity2=55

because I don't know how many products are queried.因为我不知道查询了多少产品。

Or like this:或者像这样:

/produkt?product=1,44&product=2,55

because how can the client know that before the comma there is the productId and after the comma the quantity.因为客户怎么知道逗号前是productId,逗号后是数量。

Does anyone have other solutions?有人有其他解决方案吗? Or is it not RESTful to offer the possibility to query multiple products with one request?还是提供通过一个请求查询多个产品的可能性不是 RESTful? Is it better to offer the possibility to query just one product with the associated quantity and if the client wants to query more products, they should send more requests?提供仅查询具有关联数量的一种产品的可能性是否更好,如果客户想要查询更多产品,他们应该发送更多请求?

Here is one idea to pass a parameter:这是传递参数的一种想法:

/products?productDetail=[{"key":"key0","value":"key1"},{"key":"key2","value":"key2"},{"key":"key3","value":"key3"}]

where在哪里

[{"key":"key0","value":"key1"},{"key":"key2","value":"key2"},{"key":"key3","value":"key3"}]

is a JSON representation of the List<kv> classList<kv>类的 JSON 表示

class kv {
    String key;
    String value;


    public kv(String key, String value) {
        super();
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

so you can easily convert query parameter productDetail in to List<kv> using因此您可以轻松地将查询参数productDetail转换为List<kv>使用

new Gson().fromJson(productDetail,kv.class);

than you can easily iterate all elements.比你可以轻松地迭代所有元素。

Another suggestion is, if you don't know how many products are queried then use a POST request for this.另一个建议是,如果您不知道查询了多少产品,请为此使用POST请求。

I would expand upon your second suggestion a little by adding explicit names for the parts of your product, and using semicolons in place of commas to separate product attributes, since the order does not matter * .我会通过为产品的各个部分添加明确的名称并使用分号代替逗号来分隔产品属性来稍微扩展您的第二个建议,因为顺序无关紧要*

/products?id=1;qty=44&qty=55;id=2

Note how id and qty are switched around for the second product, because the order of attributes does not matter.请注意第二个产品的idqty是如何切换的,因为属性的顺序无关紧要。


* There is a convention to use commas when the order is important, and semicolons when the order is not important. *当顺序重要时使用逗号,当顺序不重要时使用分号。

The most idiomatic HTTP GET way I can think to do it:我能想到的最惯用的 HTTP GET 方式:

/produkt?productId=1&productquantity=44&productId=2&productquantity=55

I ran into a similar situation recently where Clients needed to be able to search by "Product Attributes" which are defined arbitrarily for each Product.我最近遇到了类似的情况,客户需要能够通过为每个产品任意定义的“产品属性”进行搜索。 Clients also needed to be able to create these links easily and pass them around via email, etc. I didn't want to create a custom query string because I didn't want to create a custom parser and it adds an extra step where the Client could make a mistake.客户还需要能够轻松创建这些链接并通过电子邮件等方式传递它们。我不想创建自定义查询字符串,因为我不想创建自定义解析器,它添加了一个额外的步骤,其中客户可能会犯错误。 And I didn't want to pass json as it relied on the Client to generate that JSON in some cases.而且我不想传递 json,因为它在某些情况下依赖客户端生成该 JSON。

While the HTTP Spec doesn't take a hard stance on multiple values per parameter, every example I can find indicates that order is preserved.虽然 HTTP 规范并未对每个参数的多个值采取强硬立场,但我能找到的每个示例都表明保留了顺序。 Java example: Java 示例:

String[] productIds = this.request.getParameterValues("productId");
String[] productQuantities = this.request.getParameterValues("productquantity");

productIds[0]; // 1
productQuantities[0]; // 44
productIds[1]; // 2
productQuantities[1]; // 55

I'll leave error and index range checking as an exercise for the reader.我将把错误和索引范围检查留给读者作为练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM