简体   繁体   English

关于RESTful Web服务的2个问题

[英]2 questions about RESTful Web Services

I am new to RESTful web services. 我是RESTful Web服务的新手。 I have the following 2 questions: 我有以下两个问题:

  1. Are GET, POST, DELETE, PUT, TRACE, HEAD, OPTIONS, the only verbs in Http that I can use for RESTful web services? GET,POST,DELETE,PUT,TRACE,HEAD,OPTIONS是Httpful中唯一可用于RESTful Web服务的动词吗?

  2. How do I create and use a custom verb? 如何创建和使用自定义动词?

I'm using Java and Jersey for creating my RESTful web services. 我正在使用Java和Jersey来创建我的RESTful Web服务。

The answer to question 1 is, yes as they are restricted by the HTTP specification. 问题1的答案是,是的,因为它们受HTTP规范的限制。 However as a matter of practice, most REST applications use only GET and POST, as these are most widely supported by all of the Internet infrastructure. 但是,实际上,大多数REST应用程序仅使用GET和POST,因为这些应用程序受到所有Internet基础结构的最广泛支持。 And then the answer to question two is no, you can't create a custom verb. 然后问题二的答案是否定的,你不能创建一个自定义动词。

The thing you have to consider in your use of the HTTP verbs is that a GET should have no side effects, as the client is free to resend a GET at any time (in the event a communication failure was detected). 您在使用HTTP谓词时必须考虑的事情是GET应该没有副作用,因为客户端可以随时重新发送GET(如果检测到通信故障)。 A POST however can be sent by the client at most once, so this should be used for anything that causes a change that cannot be repeated (like an insert). 但是,POST最多只能由客户端发送一次,因此,POST应当用于导致无法重复的更改的任何内容(例如插入)。

Normally you would define what "verb" you want in your application as part of the URL, not as the HTTP verb. 通常,您将在应用程序中将“动词”定义为URL的一部分,而不是HTTP动词。

Then how do I provide the 10 actions with only 7 verbs? 那么如何仅用7个动词提供10个动作呢?

The idea behind web services is to focus on the objects , not the verbs. Web服务背后的想法是关注对象 ,而不是动词。

Your actions either "Create" ("POST"), "Retrieve" ("GET"), "Update" ("PUT") or "Remove" ("DELETE") the objects . 您的操作“创建”(“POST”),“检索”(“GET”),“更新”(“PUT”)或“删除”(“删除”) 对象

Doesn't each action go under a separate verb? 每个动作不是都放在单独的动词下吗?

No. You can have all the objects you want. 不。您可以拥有所需的所有对象。 You only need four verbs to create, find, change and remove objects. 您只需要四个动词即可创建,查找,更改和删除对象。

Or I'm wrong and can use conditionals to provide several actions under a single verb? 或者我错了,可以使用条件语在单个动词下提供几个动作?

No. You can make a create ("POST") request which can, in turn, create a number of individual objects. 不可以。您可以创建一个创建(“POST”)请求,该请求又可以创建许多单个对象。

In general how do others design their application such that they don't need extra verbs even if they need to provide a 100 different actions? 一般来说,其他人如何设计他们的应用程序,即使他们需要提供100种不同的动作,他们也不需要额外的动词?

You focus on the objects . 您专注于对象 Objects are created, retrieved, updated and deleted. 创建,检索,更新和删除对象。

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

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