java 如何为 HTTP GET 的多个 Key-Value 参数设计 REST URI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13788830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to design REST URI for multiple Key-Value params of HTTP GET
提问by joshi737
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? 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?
有人有其他解决方案吗?或者提供通过一个请求查询多个产品的可能性不是 RESTful 吗?提供仅查询具有关联数量的一种产品的可能性是否更好,如果客户想要查询更多产品,他们应该发送更多请求?
采纳答案by dasblinkenlight
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.
请注意如何为第二个产品切换id
和qty
,因为属性的顺序无关紧要。
**有约定在顺序重要时使用逗号,在顺序不重要时使用分号。
回答by Yogesh Prajapati
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>
class
是的JSON表示List<kv>
类
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
请求。