2013-05-14 60 views
0

REST似乎最關注資源集合 - 事物列表。應該如何模擬一個在系統中存在一次的東西?更具體的東西很簡單。假設建模系統是一個教室,它有學生,一位教師和一個開放或關閉的門。一個人如何塑造門?我在想它會是這樣的:如何在REST API中爲全局資源建模?

GET and POST operations are supported. 
GET https://<ipaddress>/classroom/door_status 
    Returns 200 if successful, with a response containing: 
     DoorStatus - String - Value of door status, either "Open" or "Closed" 
POST https://<ipaddress>/classroom/door_status 
    Specify the attribute of: 
     DoorStatus - String - Value of desired door status, "Open" or "Closed" 
    Returns 201 if the status was successfully modified. 

DELETE總是會失敗。

一間教室當然可以有多個門,但暫時忍受着我。當然,有教室的建築往往有多個教室。再次忍耐着我。

接下來,我們可能會爲教室添加一個light_status資源。鑑於這很可能,我們應該從一個「global_properties」資源開始,該資源將具有DoorStatus和LightStatus屬性。

感謝您的建議,幫助或(尤其是)示例。 ... Alan

回答

0

我不認爲REST中有任何東西會導致單實例實體非法或不合需要。這就是說,在你的具體的例子,你需要的,如果門和光評價:

  1. 相關但另有獨立於類自己的實體,
  2. 只是屬性或包含的對象該班級沒有自己的身份,其存在取決於班級的存在

第二種選擇是對我來說似乎更合理的一種。如果我們接受它,那麼您可以返回燈光和門狀態作爲類屬性的一部分:

GET /class -- Returns the class attributes, including light and door status 
PUT /class -- updates class attributes, including light and door status