簡體   English   中英

使用RESTful處理子對象上的CRUD

[英]Handling CRUD on child objects with RESTful

如果給出以下xml請求,則該XML請求通過PUT發送到名為/ catalog的RESTful API端點(意圖更新目錄)。

如果用戶提供了請求中目錄中當時不存在的其他<book> ,則端點應該“創建”它們嗎? 還是應該忽略它們,並使用其他端點/ books來創建它們。

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="bk109">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="bk110">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="bk111">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

我在SO上發現了很多內容,這些內容討論了PUT與POST以及對根對象執行CRUD操作的最佳實踐,但是我花了一整夜的時間來尋找有關應如何處理子對象的信息。

根據http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html ,我不清楚這是否適用於子對象?

9.6放置

PUT方法請求將封閉的實體存儲在提供的Request-URI下。 如果Request-URI引用了已經存在的資源,則應將封閉的實體視為駐留在原始服務器上的實體的修改版本。 如果Request-URI沒有指向現有資源,並且請求用戶代理能夠將該URI定義為新資源,則原始服務器可以使用該URI創建資源。 如果創建了新資源,則原始服務器務必通過201(已創建)響應通知用戶代理。 如果修改了現有資源,則應發送200(確定)或204(無內容)響應代碼以指示請求已成功完成。 如果無法使用Request-URI創建或修改資源,則應給出反映問題性質的適當錯誤響應。 實體的接收者不得忽略其無法理解或實現的任何Content-*(例如Content-Range)報頭,並且在這種情況下必須返回501(未實現)響應。

根據我對REST的理解,書籍是資源,應該通過它們自己的端點進行處理,並且對目錄的更新僅應用於將現有書籍添加到目錄中,而不是創建它們。

因此,在這種情況下,不存在的書將被忽略/不添加到目錄中,直到提交創建請求以創建書。

這意味着用戶將發送一個POST / book來創建該書,然后向/ catalog發送一個PUT並將請求中的書添加到目錄中。

你是對的。 您在這里有兩種不同的資源。 一個叫做書,另一個叫做目錄。 您必須設計兩個不同的URI來尋址每個URI。 因此,在這種情況下,我將創建兩個不同的資源來處理此問題。 第一個資源將被命名為/ catalogs,並且您必須構建它以僅接收某人想要與此目錄關聯的書籍ID(一個或多個)。 您將擁有:

Endpoint -> https://yourapi.com/catalogs

POST to https://yourapi.com/catalogs --> creates a new catalog.
PUT to https://yourapi.com/catalogs/1 --> updates the catalog with ID = 1.
PUT to https://yourapi.com/catalogs/2 where id 2 doesn't exist --> creates a new catalog with id=2

您必須將下面的XML發送到上面指出的端點。

<?xml version="1.0"?>
<catalog>
    <book id="bk101"/>
    <book id="bk102"/>
    <book id="bk103"/>
    <book id="bk104"/>
<catalog>

第二個資源應該是/ books,並且您必須使用它來創建,刪除,更新和列出書籍。 創建書籍后,即可將其與目錄相關聯。 您不應該使用資源目錄以這種方式創建一本書。 在我看來,這沒有任何意義。

您試圖做的是批量/批量更新。 據我所知。 我們對此沒有達成共識。 這里有很多問題:

  • 應該通過更新整個資源來使用PUT。 由於此處的資源為/catalog (因此存儲中的每一本書),因此您必須提供整個目錄的表示形式。 您應該考慮使用PATCH,它可以用於部分更新。
  • PUT是冪等的。 因此,如果您發送兩次PUT,則不會有任何副作用。 這就是為什么僅當用戶提供新資源的ID時才可以使用PUT進行創建的原因。 (否則,您將創建兩次新資源。)PATCH不是冪等的,因此2:0對應PATCH。

您可以在此處找到有關PATCH的描述: HTTP的PATCH方法

但是,對於PATCH,封閉的實體包含一組指令,這些指令描述了應如何修改當前駐留在源服務器上的資源以產生新版本。 PATCH方法影響由Request-URI標識的資源,並且可能對其他資源也有副作用。 也就是說,可以通過應用PATCH來創建新資源或修改現有資源。

因此, PATCH /catalog和您的XML都可以。 您可以決定是否允許用戶提供新資源的ID,或者可以在服務器端生成它。

OFC。 您有其他選擇,例如,您可以使用POST /catalog發送書籍收藏,因此將創建多個書籍資源。 您可以使用PUT /catalog/?id="bk112,bk113,..."更新特定的書籍集合。 如前所述,另一種創建所有內容的方法。

請注意,我們在談論超鏈接( METHOD /resource-id?query <data /> + link metadata: eg link relation )。 因此,您應該考慮將鏈接添加到由GET返回的資源表示形式,並且還可能使用超媒體格式,例如HAL + XML或ATOM + XML。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM