简体   繁体   English

在为我们的应用程序形成REST URL时,如何区分与同一对象相关的两个服务?

[英]While forming a REST URL for our application, how should we differentiate two services related to the same object?

need two services related to the object "item". 需要与对象“ item”相关的两个服务。

  1. To get the details of the item. 获取项目的详细信息。 Though it is a method to get data, I will be using POST since my pathParam/MatrixParam list is too long(Say, purchaserIds provided in the matrixParam is used to fetch the items they have bought). 尽管这是一种获取数据的方法,但由于pathParam / MatrixParam列表太长(我会使用MatrixParam中提供的purchaseIds来获取他们购买的商品),所以我将使用POST。 To avoid "URI too long" exception, I made it POST. 为了避免出现“ URI太长”异常,我将其设为POST。

  2. To save the item with some details. 保存带有一些细节的项目。 This will also be a POST method since it is saving/updating. 这也将是POST方法,因为它正在保存/更新。

Now, both services will have the same url http://...../item and both will have httpMethod as POST. 现在,这两个服务将具有相同的URL http://...../item,并且都将具有httpMethod作为POST。

How can we differentiate these 2 services by the URL? 我们如何通过URL区分这两种服务? Is it right to make it " http://.../item/save " and ".../item/get"? 将其设置为“ http://.../item/save ”和“ ... / item / get”是否正确?

If your URLs would end up too long, it sounds like your URL design is wrong - GET against http://.../items/1/purchasers/ would (for example) be a decent URL to return all the people who have bought item 1, then to update item 1, you would PUT to http://.../items/1/ and to create an item you would POST to http://.../items/ 如果您的网址结尾太长,听起来您的网址设计有误-例如,针对http://.../items/1/purchasers/ GET http://.../items/1/purchasers/将是一个不错的网址,以返回所有拥有该网址的用户购买了第1件,然后更新第1件,则将其http://.../items/1/http://.../items/1/并创建一个商品,然后将其发布到http://.../items/

The URL scheme you decide does not sound RESTful, as you're using it to describe actions rather than resources - the URL should identify the resource you are accessing rather than what to do to it. 您确定的URL方案听起来不是RESTful的,因为您正在使用它来描述操作而不是资源-URL应该标识您正在访问的资源,而不是要做什么。

To get the details of item you will need to use GET method with item id as path parameter. 要获取商品的详细信息,您将需要使用带有商品ID作为路径参数的GET方法。

http://....../itemid HTTP://....../itemid

using POST for GET will defeat the purpose and entire design of the api will be disturbed. 使用POST进行GET将无法达到目的,并且会干扰api的整个设计。

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

相关问题 我们的应用程序中可以同时包含RESTful和SOAP Web服务吗 - Can we have both RESTful and SOAP Web Services in our application 在两台不同的计算机上创建RMI应用程序时,客户端和服务器应在哪里定义接口客户端或服务器端? - When creating an RMI application on two different machines Client and Server Where should we Define our Interface Client side or Server side? 形成和发送URL时出现问题 - Issue while forming and URL and sending it 如何检查我们的 class 中有 object - How to check we have object in our class 如何区分同一类的两个方法参数? - How to differentiate two method parameters of the same class? 如何区分同一jsp上存在的两个iframe - How to differentiate two iframes present on the same jsp 应如何组织提供 REST web 服务的 JEE6 企业应用程序? - How a JEE6 enterprise application that offers REST web services should be organized? Android-形成一个指定端口号的URL对象 - Android - Forming an URL object specifying a port number 如何使用Java在桌面应用程序上使用休息服务? - How to consume rest services on desktop application with Java? 如何区分同一自定义接口方法返回的两个数据集 - How to differentiate two data sets returned by the same custom interface method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM