2012-05-15 42 views
3

我想在Java中開發我的第一個restful服務,並且在將這些方法映射到CRUD功能時遇到了一些麻煩。將寧靜的體系結構映射到CRUD功能

我的URI結構如下,並映射到基本的數據庫結構:

/數據庫/ {架構}/{表}/

/數據庫是靜態

{架構}和{TABLE}是動態的,在path參數反應

這是我有:

Method - URI  - DATA  - Comment 
--------------------------------------------------------------------- 
GET - /databases - none  - returns a list of databases 
POST - /databases - database1 - creates a database named database1 
DELETE - /databases - database1 - deletes the database1 database 
PUT - /databases - daatbase1 - updates database1 

目前在上面的例子中,我通過數據庫名稱作爲JSON對象。但是,我不確定這是否正確。如果我不是做這(使用DELETE方法爲例):

Method - URI     - DATA - Comment 
--------------------------------------------------------------------- 
DELETE - /databases/database1 - none - deletes the database with the same name 

如果這是正確的方法,我需要通過額外的數據將在下面接着是正確的:

Method - URI     - DATA  - Comment 
--------------------------------------------------------------------- 
DELETE - /databases/database1 - some data - deletes the database with the same name 

任何意見將不勝感激

回答

1

REST是一個接口到您的域。因此,如果你想公開數據庫那麼CRUD可能會工作。但是還有更多的REST(見下文)

REST-AFarians會反對你的服務是RESTful,因爲如果不適合其中一個關鍵約束:超媒體約束。但是,如果您將鏈接添加到您的服務將生成/提供的文檔(超媒體),則可以解決該問題。請參閱Hypermedia constrain。在此之後,您的用戶將通過鏈接和表單來更改應用程序中的內容。 (在你的例子中數據庫,表和行):

- GET /database -> List of databases 
- GET /database/{name} -> List of tables 
- GET /database/{name}/{table}?page=1 -> First set of rows in table XXXXX 
- POST /database/{name}/{table} -> Create a record 
- PUT /database/{name}/{table}/{PK} -> Update a record 
- DELETE /database/{name}/{table}/{PK} -> Send the record to the big PC in the sky.. 

不要忘了添加鏈接到你的文件!

對RUD使用REST就像是把它放在一個Straitjacket :):你的URI可以表示任何概念。因此,如何嘗試根據您希望服務或Web應用程序執行的下屬資源(功能)來公開一些更具創造性/豐富的URI。

在看看這篇大文章:How to GET a Cup of Coffee