且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

与实体不对应的 RESTful 操作/服务?

更新时间:2022-05-07 23:24:15

我认为您应该停止将资源视为数据库实体的同义词.是的,它们通常是相关的,但资源实际上只是您领域中的一个可寻址概念.我认为将资源视为您在使用浏览器时在网络上看到的内容(列表、项目、帖子、评论、图像等)会更有用.

I think you should stop thinking about resources as synonyms for database entities. Yes, they're often related, but a resource is really just an addressable concept in your domain. I think it's much more useful to think about resources as the things you see on the web when you use your browser (lists, items, posts, comments, images, whatever).

但是您将如何执行全部清除"命令?

But how would you do a "clear all" command?

我不确定为什么 DELETE/products/milk 意味着删除牛奶类别本身,但如果您愿意:

I'm not sure why DELETE /products/milk implies the deletion of the milk category itself, but if you'd rather:

DELETE /products?category=milk

DELETE 并不意味着删除单个数据库实体.它意味着删除单个资源.并且/products?category=milk"(或/products/milk",就此而言)标识单个资源.如果你能得到它,你可以删除它.

DELETE doesn't imply the deletion of a single database entity. It implies the deletion of a single resource. And "/products?category=milk" (or "/products/milk", for that matter) identifies a single resource. If you can GET it, you can DELETE it.

如果您想同时实现这两个目标怎么办?

And what if you want to accomplish both?

这个怎么样?

DELETE /product-categories/milk

Ruby on Rails 流行的一个技巧是为任何 PUT/POST/DELETE 操作提供一个表单(使用 GET).因此,对于这些删除,您可能需要提供这样的表单:

One trick that became popular with Ruby on Rails is to provide a form (using GET) for any PUT/POST/DELETE operation. So, for these deletes, you may want to provide a form like this:

GET /product-categories/milk/delete

在那种表单中(想想 HTML),您可以询问您的用户是否真的可以删除整个类别.(请不要注意 HTML 表单与 RESTful Web 服务并不真正兼容的概念.HTML是一种非常成功的与网络资源交互的格式,一个设计良好的 AJAX 应用程序可能首先作为一个设计良好的 HTML 应用程序工作.有一些细节需要解决以支持浏览器和其他客户端,但他们都是合法的 REST 客户端.)

In that form (think HTML), you could ask your user if it's really OK to delete the whole category.(Please don't pay attention to the notion that HTML forms aren't really compatible with RESTful web services. HTML is a pretty successful format for interacting with resources on the web, and a well-designed AJAX application probably first works as a well-designed HTML application. There are some details that need to be worked out to support both browsers and your other clients, but they're all legitimate REST clients.)

如何禁用网站?

有很多方法可以做到这一点.只需将带有禁用标志的 PUT 放到/sites/***.com 即可.

There are lots of ways to do this. Just a PUT to /sites/***.com with a flag to disable might work.

最后,您如何协调 HTML 表单与 RESTful?

Finally, how do you reconcile HTML Forms with RESTful?

您无法真正从浏览器执行 HTTP PUT 或 DELETE,但您可以在表单中提供一个隐藏字段来伪造它:

You can't really do an HTTP PUT or DELETE from the browser, but you can provide a hidden field in your form to fake it:

<input type="hidden" name="_method" value="PUT" />

只要您的路由引擎支持它,这是将浏览器帖子路由到适当处理程序的好方法(我还看到人们对非 HTML 客户端使用 X-HTTP-Method-Override 标头而没有完整的支持 HTTP 动词).

As long as your routing engine supports it, this is a good way to route a browser post to the appropriate handler (I've also seen people use an X-HTTP-Method-Override header for non-HTML clients without full support for HTTP verbs).

如果您有兴趣深入研究,我建议您先阅读 Web 服务手册.另外,请查看理查森成熟度模型.请记住,REST 就像网络的其他部分一样.如果没有链接,它不会很有用.为您的客户提供出行方式.

If you're interested in digging in, I recommend the Web Services Cookbook as a starter. Also, take a look at the Richardson Maturity Model. And remember that REST is just like the rest of the web. It wouldn't be very useful without the links. Provide your clients with a way to get around.

<a href="/products/milk/delete" rel="delete" />

<atom:link href="/products/milk/delete" rel="delete" />