2012-04-07 68 views
4

假設我想設計一個用於管理列表的REST存儲。 列表條目會是這個樣子:設計一個REST資源來管理列表

<listentry> 
    <position>0</position>    <!-- position in the list --> 
    <data>interesting information</data> <!-- entry data --> 
</listentry> 

我會設計這樣的資源:

GET /list/   // returns all list entries 
GET /list/{position} // returns the list entry at {position} 
DELETE /list/{position} // delete list entry at {position} 

PUT /list/first  // update first list entry 
PUT /list/last  // update last list entry 
PUT /list/{position} // update entry at {position} 

POST /list/last  // inserts a new list entry at last position 
POST /list/first  // inserts a new list entry at first position 

POST /list/{position} // inserts a new list entry at {position} and moves all 
         // entries down the list starting from the entry that 
         // was at {position} before the insertion. 

這是合法的REST資源?如果沒有,有沒有辦法設計一個休息資源,以便它可以管理一個列表?

編輯

感謝您輸入它definetly幫助。 我同意nategood和darrel使用第一個和最後一個作爲特殊標識符是完全合法的(另請參見我的question)。當然,我可以在沒有Saintedlama建議的那些魔術標識符的情況下做到這一點,但是這可能會使我無法將這些標識符用於我現在想提出的發佈請求中。

在再次考慮設計時,我想添加兩個額外的功能到我的資源設計建議。

POST /list/{position1}/swap/{position2} // swap the position of two elements 
POST /list/{position1}/move/{position2} // move the element at {position1} to 
              // {position2} and move all entries 
              // down the list starting from the 
              // entry that was at {position2} 

//possible uses 
POST /list/first/swap/last    // swap first with last element 
POST /list/42/swap/2      // swap element 42 with element 2 
POST /list/first/move/42     // move first element to position 42 
// you get the idea ... 

您怎麼看?

回答

2

對於我的REST理解,您的資源設計完全可以。通過引入一條簡單的規則:如果沒有提供位置,則最後一個項目是更新或者該項目插入到最後位置,您可以通過刪除第一個末尾魔術索引功能來改進設計。如果你引入這個規則,你不需要第一個和最後一個。首先只是表示索引0的一個魔術字符串,由於上述規則,last是過時的。

正如@miku所說,你的物品可能是他們自己的資源。如果您計劃一個更通用的資源列表設計,您需要在一個列表中管理不同的資源類型(例如列表可以管理任務,會議,約會),則列表項目可以再次引用(使用資源url)到項目資源。使用這種引用方法,您可以將列表保留功能與列表項表示完全分離。

編輯:

這個問題是得到一個移動的目標:)

你可以建模爲資源和子資源的操作,您創建,莫名其妙地喜歡這一切位置相關的操作:

POST /list/positions/swap/0/2 // swap the position of two elements 
POST /list/positions/move/1/0 // move the element at 1 to 0 

如果操作成功或不成功,此位置資源可能會返回(HTTP)狀態,「操作」資源的句柄(通過位置標題)可以檢查t的狀態他希望移動的操作,交換異步,返回給你所有列表位置操作的日誌的資源。

從書RESTful Web Services中盜取將資源模型化爲資源的想法,其中作者將兩個銀行賬戶之間的轉賬交易建模爲資源。

+0

因此,您將保存對列表中管理的資源的引用? 順便說一句,爲消除第一個和最後一個解決方法的好主意,但請參閱我的編輯使用特殊標識符。 – Zounadire 2012-04-08 18:47:33

+0

抱歉關於編輯後期,但有時候想法會增長:P再次強烈建議,我特別喜歡返回句柄來檢查操作狀態的想法。 – Zounadire 2012-04-09 21:33:29

1

只是一些想法:

  • 網址,像... /第一或... /最後是那種RPC-ISH
  • 列表項目似乎是一個資源的自己的,所以它最終應該解決的一個,如:

    GET /list/3/item/2 
    
  • REST isn't easy,需要時間環繞nested resources等人的頭上。

+1

什麼是關於'/ first'和'/ last'的「RPC-ish」?它們在RESTful API中的URI很好。我可能只建議這些資源有一個'Location'頭,該頭指向相同資源的規範URI(例如,在'GET/first'的頭部返回'Location:/ list/0')。 – nategood 2012-04-08 00:30:29

+0

@nategood;我不是REST專家(我仍然在閱讀Fieldings的論文......) - 然而有一件事讓我想起來:URLs應該有點穩定(因爲它們是*識別資源)和'/ first'或'/最後'可能會頻繁改變。無論如何,對於RESTful的東西/第一次和/最後可能會正常工作。 – miku 2012-04-08 00:36:55

+0

@miku第一個和最後一個的概念是完全穩定的。內容可能會改變,但這是完全正常的。 – 2012-04-08 02:04:24