如何在REST中传递复杂的查询? [英] How to pass complex queries in REST?

查看:124
本文介绍了如何在REST中传递复杂的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确理解,那么在其余样式中,应该使用get方法将每个查询(即,对每个资源的每个操作都不会修改资源状态的状态)编码到查询字符串中,而没有任何正文.

If I understand correctly, in rest style, every query (that is, every action on every resource that does not modifies the resource's state) should be encoded in the query string, using a get method, with no body at all.

我说得对吗?

好吧,我有几个应用程序,它们通过由Visual Basic 6组件处理的XML消息与数据库进行通信.

Well, I have several applications that communicate with the db thru an XML message that is handled by a Visual Basic 6 component.

查询消息是这样的

<xml>
  <service>account</service>
  <resource>invoice</resource>
  <action>query</action>
  <parameters>
    <page>1</page>
    <page_len>10</page_len>
    <order>date</order>
    <fields>*</fields>
    <conditions>
      <date>2009-01-01..2009-01-31</date>
      <customer_id>24</customer_id>
    </conditions>
  </parameters>
</xml>

现在,我们正在重新设计XML消息,我们希望以一种可以轻松将它们映射到RESTful接口的方式进行操作.

Right now we are in the process of redesigning our XML messages, and we'd like to do that in such a way that they could be easily mapped to a RESTful interface.

在前面的示例中,我们需要"conditions"标签,以防止参数与条件之间发生冲突(即,如果我有一个名为"order","page"或类似名称的字段,会发生什么情况.

In the previous example, we need the "conditions" tags in order to prevent collisions between the parameters and the conditions (i.e., what happens if I have a field named "order", "page" or something like that...

我们虽然要发送带有前缀的参数,例如

We though about sending the parameters with a prefix, something like

http://account/invoice/?_page=1&_page_len=10&_order=date&_fields=*&date=2009-01-01..2009-01-31&customer_id=24

XML就像

[...]
    <_order>date</_order>
    <_fields>*</_fields>
    <date>2009-01-01..2009-01-31</date>
    <customer_id>24</customer_id>
[...]

我们正在尝试为Crud操作定义一些非常简单的XML格式,并且可以将生成的XML轻松映射到rest或JSON.

We are trying to define some really simple XML format for crud operations, and that the resulting XML could be easily mapped to rest or JSON.

您将如何在其余应用程序中映射这种查询?有没有定义一些标准?或带有粗糙休息/XML/JSON示例的页面?返回错误或嵌套数据集如何?

How would you map that kind of query in a rest application? Is there some standard defined? or some page with crud rest / XML /JSON samples? how about returning error, or nested datasets?

非常感谢.

推荐答案

恕我直言,为了使您的系统真正实现RESTful,您必须重新考虑将要发送的所有消息/查询.

IMHO in order to make your system truly RESTful you must rethink all the messages/queries that you will send.

此部分:

<conditions>
  <date>2009-01-01..2009-01-31</date>
  <customer_id>24</customer_id>
</conditions>

是棘手的部分.您还有什么其他条件?有很多吗这个特定的示例使我认为您可以将发票视为客户的子资源.当我休息时,我总是尝试识别路径中的资源,如果查询stil需要任何参数,我会将其移至查询字符串.所以我会写这样的东西:

is the tricky part. What kind of other conditions do you have? Are there many? This particular example makes me think that you can treat invoices as a subresource of a customer. When I do rest I always try to identify resource in the path and if a query stil needs any parameters, I move them to query string. So I'd write something like this:

GET /customers/24/invoices?start_date=2009-01-01&end_date=2009-01-31

考虑资源之间的关系.假设我们通过一对多关系获得了与资源类型Foo相关的资源类型Bar.在这种情况下,您可以这样查询这种关系:GET /foo/123/bar并添加查询字符串参数以对其进行过滤.当您想以涉及与其他资源的关系的方式对其进行筛选时,问题就开始了.恕我直言,这意味着您的资源设计不是真正的RESTful.

Think about relations between your resources. Let's say we have resource type Foo related resource type Bar by a -to-many relation. In this case you can ask about this relation like this: GET /foo/123/bar and add query string parameters to filter it. The problem begins when you want to filter it in a way that involves relations to other resources. IMHO this means that your resource design isn't truly RESTful.

这篇关于如何在REST中传递复杂的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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