Java 在 RESTful Web 服务中使用 GET、PUT 和 DELETE HTTP 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21094726/
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
Usage of GET, PUT and DELETE HTTP methods in RESTful web-services
提问by Chillax
I am working on a Ticket Reservation application. I am required to build a web-service which takes in request for cancellation, checks with a downstream system (via SOAP), whether it can be cancelled, and returns the appropriate response to the calling system after updating my tables.
我正在处理机票预订应用程序。我需要构建一个 web 服务,它接受取消请求,检查下游系统(通过 SOAP),是否可以取消,并在更新我的表后向调用系统返回适当的响应。
I have to build a bunch of similar web-services for cancellation, rescheduling, et for which I'm planning to use RESTful web-services
我必须构建一堆类似的网络服务来取消、重新安排等,我打算使用 RESTful 网络服务
This I'm planning to achieve using the POST method of the REST API, using JAX-RS. I could see that all the web-services I'm going to build suit the POST verb, and the other HTTP methods (GET, POST and DELETE) doesn't seem like they need to be used here. In that case what is the actual use case of these HTTP methods in REST? Am I right in using only POST?
我计划使用 REST API 的 POST 方法,使用 JAX-RS 来实现这一点。我可以看到我要构建的所有 Web 服务都适合 POST 动词,而其他 HTTP 方法(GET、POST 和 DELETE)似乎不需要在这里使用。在这种情况下,这些 HTTP 方法在 REST 中的实际用例是什么?我只使用 POST 是否正确?
回答by Vidya
For the purpose of a cancellation, I would actually use the DELETE method. In the case of a successful cancellation, return a 200 with either response body confirming success or a response body containing the details of the reservation. Or a 204 if you don't want to return a body.
出于取消的目的,我实际上会使用 DELETE 方法。在成功取消的情况下,返回 200 与确认成功的响应正文或包含预订详细信息的响应正文。或者如果您不想返回尸体,则为 204。
If it can't be canceled, you have your choice from the 400 series of errors (e.g. 404 if there is no such reservation, 403 if the request is forbidden, etc.) or the 500 series if there is a bug or malfunction.
如果无法取消,您可以选择400系列错误(例如404如果没有这样的预订,403如果请求被禁止等)或500系列如果有错误或故障。
You will certainly use the other HTTP verbs for all the other reservation actions--making a reservation, changing it, etc.
您肯定会将其他 HTTP 动词用于所有其他预订操作——进行预订、更改预订等。
回答by Xavier Coulon
Consider your Ticket Reservations as your application resources. Clients to your application would:
将您的机票预订视为您的应用程序资源。您的应用程序的客户端将:
- submit
GET
request to retrieve a representation (XML, JSON or anything else) one or many ticket reservations (given the URL they use, eg:/tickets
to retrieve all of them - or all they are allowed to see,/tickets/1
to retrieve a representation of the Ticket Reservation whose id is1
,tickets/?start=0&size=10
for paginated content, etc.) - submit
POST
requests on/tickets
to create a new reservation (and the response would provide the client with alocation
giving the location of the created reservation) - submit a
PUT
request to/tickets/1
(with a representation of the new reservation in the request body) to update the reservation identified by1
- submit a
DELETE
request to/tickets/1
to delete the reservation identified by1
- 提交
GET
请求以检索表示(XML、JSON 或其他任何内容)一个或多个机票预订(给定他们使用的 URL,例如:/tickets
检索所有这些 - 或允许他们看到的所有内容,/tickets/1
检索机票预订的表示)其 id 是1
,tickets/?start=0&size=10
用于分页内容等) - 提交创建新预订的
POST
请求/tickets
(响应将向客户端提供location
所创建预订的位置) - 提交
PUT
请求/tickets/1
(在请求正文中使用新保留的表示)以更新由1
- 提交
DELETE
请求以/tickets/1
删除由1
The main idea behind the use of such verbs is that the URLs should identify resources (and not actions), and the HTTP verb would be the action.
使用此类动词背后的主要思想是 URL 应标识资源(而不是动作),而 HTTP 动词将是动作。
If you use everything with POST
, you'll be more or less doing SOAP.
如果您将所有内容与 一起使用POST
,您将或多或少地使用 SOAP。
This article will give you more information on hat I tried to hihlight above: http://martinfowler.com/articles/richardsonMaturityModel.html
这篇文章将为您提供有关我在上面尝试强调的帽子的更多信息:http://martinfowler.com/articles/richardsonMaturityModel.html
回答by Angular University
Your REST resource is the ticket. Use GETs to read the ticket, PUT to modify the ticket state, and DELETE to do a logical delete (cancellation).
您的 REST 资源就是票证。使用 GET 读取票证,使用 PUT 修改票证状态,使用 DELETE 进行逻辑删除(取消)。
For any other operation, use the wildcard operation POST.
对于任何其他操作,请使用通配符操作 POST。
Have a look at this blog post Brief Introduction to REST, where the author goes through the core REST principles.
回答by J?rn Wildt
There's quite a lot to say about designing RESTful applications. You really should read one or more of these books: "Restful Web Services" - http://amzn.com/0596529260, "RESTful Web APIs" - http://amzn.com/B00F5BS966, "REST in Practice: Hypermedia and Systems Architecture" - http://amzn.com/B0046RERXYor "RESTful Web Services Cookbook" - http://amzn.com/B0043D2ESQ.
关于设计 RESTful 应用程序有很多话要说。您确实应该阅读其中的一本或多本书:“Restful Web 服务”- http://amzn.com/0596529260、“RESTfulWeb API”- http://amzn.com/B00F5BS966、“RESTin Practice: Hypermedia and系统架构” - http://amzn.com/B0046RERXY或“RESTful Web 服务手册” - http://amzn.com/B0043D2ESQ。
Get a cup of coffee and enjoy some reading :-)
喝杯咖啡,享受一些阅读:-)