我对 REST 有什么不了解? [英] What am I not understanding about REST?

查看:27
本文介绍了我对 REST 有什么不了解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个框架,并希望使用它构建的开发人员能够允许其中的一部分与其他站点共享数据并允许其他站点添加/编辑/删除数据.

例如,如果有人制作了一个包含书评、作者、引述、代码示例、评论等的网站,开发人员可以制作例如书评"对其他网站只读,评论"对其他网站可读,某些网站/用户可写.想法是使用该框架构建可以轻松与其他应用程序互连的应用程序.

我设想通过 POST 和 GET 启用与站点的所有交互,如下所示:

  • /books.php?category=ruby(返回关于 ruby​​ 的书籍的 XML 集合)
  • /books.php?id=23(返回特定书籍的 XML)
  • /books.php?action=add&title=AdvancedRuby&description=....&securityId=923847203487
  • /books.php?action=delete&id=342&securityId=923847203487

其他应用程序也可以通过这样做来发现和使用"某个网站所提供的内容:

  • /discover.php(返回所有可用公共类和操作的 XML)

这就是我让框架成为开发人员快速创建松散连接站点的一种方式所需要的全部内容.

我想知道的是,在我开始实施之前,REST 中有哪些重要/有趣的部分我还不明白我应该将哪些部分构建到框架中,例如:>

  • REST 需要 GET、POST、PUT 和 DELETE.为什么我需要PUT"和DELETE"?如果我不使用这些标准,我是否会阻止自己利用某些标准?
  • 我的discover.php"文件的功能类似于 Web 服务中的 WSDL 文件.我对 REST 的描述感到惊讶,似乎没有发现 RESTful 服务提供的服务的标准化方法,或者有没有?
  • 如果客户网站尝试例如将一本书添加到服务器网站并且没有得到任何成功"响应,它只会再试一次,直到得到响应.服务器网站不会两次添加同一本书.这是我对 REST 中数据完整性的理解,还有比这更多的内容吗?
  • 最终我希望拥有多个具有相同丰富类的站点,例如BookReview",以便客户站点能够执行如下代码:

    $bookReview = new BookReview("http://www.example.com/books.php?id=23");$book->informAuthor("关于您的书评的评论已发布在我们的网站上...");

并且服务器站点会向该评论的作者发送一封电子邮件.这种类型的交互是 RESTful 哲学的一个组成部分,还是 REST 只是通过 XML、JSON 交换数据?

解决方案

如果我不使用这些标准,我是否会无法利用这些标准?

您自己被排除在 HTTP 标准之外.当然,您可以使用 GET 参数来做同样的事情.那么它就不是 REST,而是类似于 RPC 的东西.

我可以推荐这本书 RESTful Web Services作者:伦纳德·理查森和山姆·鲁比?阅读并展示不同方法之间的差异非常有趣.

更详细地回答您的问题:由您决定走哪条路.理论上,您可以使用 RESTful 和类似 RPC 的方法来完成所有相同的事情.借助 RESTful,您可以使用底层 HTTP 协议作为协议.使用 RPC,您可以将 HTTP 用作传输手段,并将工作订单隐藏在传输数据中的某处.这会导致(不必要的)开销.

看看你的两个例子:

  • /books.php?action=add&title=AdvancedRuby&description=....&securityId=923847203487
  • /books.php?action=delete&id=342&securityId=923847203487
    • 有 POST 和 PUT 或 DELETE,为什么要有 action=add 和 action=delete?
    • 有 HTTP 身份验证.为什么要发明一个可能不太安全的 securityId?
    • 顺便说一句:您不应允许通过 GET 更改数据.这只是不应该做的事情(虽然是另一个话题;))

I'm building a framework and want developers who build with it to have the ability to allow parts of it to both share data with other sites and allow other sites to add/edit/delete data.

For example, if someone makes a site that has book reviews, authors, quotes, code examples, comments, etc. the developer could make e.g. "book reviews" read-only for other sites and "comments" readable by other sites and writable by certain sites/users. The idea is to use the framework to build applications that can easily be interconnected with other applications.

I envision enabling all interaction with the site via POST and GET which would look something like this:

  • /books.php?category=ruby (returns an XML collection of books about ruby)
  • /books.php?id=23 (returns the XML for a specific book)
  • /books.php?action=add&title=AdvancedRuby&description=....&securityId=923847203487
  • /books.php?action=delete&id=342&securityId=923847203487

Other applications could also "discover and consume" what a certain site has to offer by doing this:

  • /discover.php (returns XML of all public classes and actions available)

Really this is all I need to enable the framework to be a way for developers to quickly create loosely connected sites.

What I want to know is, before I begin implementing this, are there significant/interesting parts of REST that I do not yet understand which I should be building into the framework, e.g.:

  • REST requires GET, POST, PUT and DELETE. Why would I ever need "PUT" and "DELETE"? Am I locking myself out from taking advantage of some standard if I dont' use these?
  • My "discover.php" file functions similarly to a WSDL file in web services. I am surprised in descriptions of REST there seems to be no standardized way of discovering the services that a RESTful service offers, or is there?
  • If a client website tries to e.g. add a book to a server website and does not get any "success" response back, it would simply try again until it got a response. The server website would simply not add the same book twice. This is my understanding of data integrity in REST, is there more to it than this?
  • eventually I want to have multiple sites that have the same rich classes e.g. "BookReview" so that a client site would be able to execute code such as this:

    $bookReview = new BookReview("http://www.example.com/books.php?id=23"); $book->informAuthor("a comment about your book review was posted on our site...");

and the server site would send an e-mail off to the author of that review. Is this type of type interaction a component of the RESTful philosophy or is REST simply the exchange of data via XML, JSON?

解决方案

Am I locking myself out from taking advantage of some standard if I dont' use these?

You are yourself locking out from the HTTP standard. Of course you can use GET parameters to do the same thing. It's just not REST then, but something RPC-Like.

May I suggest the book RESTful Web Services by Leonard Richardson and Sam Ruby? It's quite fun to read and shows differences between the different approaches.

To answer your questions in a bit more detail: It's up to you to decide which way you go. In theory you can do all the same stuff with both RESTful and RPC-like approaches. With RESTful you use the underlaying HTTP protocol to be the protocol. With RPC you use HTTP just as a means of transportation and hide the work orders somewhere in the transported data. That leads to (unrequired) overhead.

Just look at two of your examples:

  • /books.php?action=add&title=AdvancedRuby&description=....&securityId=923847203487
  • /books.php?action=delete&id=342&securityId=923847203487
    • There's POST and PUT or DELETE, why have action=add and action=delete?
    • There's HTTP authentication. Why invent a - possibly less secure - securityId?
    • BTW: You shouldn't allow changes to data via GET. That's just something that shouldn't be done (another topic, though ;) )

这篇关于我对 REST 有什么不了解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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