Paul Adams
Paul Adams

Reputation: 75

Mapping restful architecture to CRUD functionality

I am trying to develop my first restful service in Java and having some trouble with mapping the methods to CRUD functionality.

My uri structure is as follows and maps to basic database structure:

/databases/{schema}/{table}/

/databases is static

{schema} and {table} are dynamic and react upon the path parameter

This is what I have:

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 

Currently in the example above I am passing through the database name as a JSON object. However, I am unsure if this is correct. Should I instead be doing this (using DELETE method as an example):

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

If this is the correct method and I needed to pass extra data would the below then be correct:

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

Any comments would be appreciated

Upvotes: 3

Views: 540

Answers (1)

Derick Schoonbee
Derick Schoonbee

Reputation: 3021

REST is an interface into your domain. Thus, if you want to expose a database then CRUD will probably work. But there is much more to REST (see below)

REST-afarians will object to your service being RESTful since if does not fit one of the key constraints: The Hypermedia Constraint. But, that can be addressed if you add links to the documents (hypermedia) that your service will generate / serve. See the Hypermedia constrain. After this your users will follow links and forms to change stuff in the application. (Database, tables and rows in your example) :

- 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..

Don't forget to add links to you documents!

Using REST for CRUD is kind of like putting it in a Straitjacket :) : Your URIs can represent any concept. Thus, how about trying to expose some more creative / rich URIs based on the underling resources (functionality) that you want your service or web app to do.

Take a look at at this great article: How to GET a Cup of Coffee

Upvotes: 1

Related Questions