RESTFul Design Patterns
Join the DZone community and get the full member experience.
Join For FreeHere I will summarize a set of RESTful design practices that I have used quite successfully.
Object Patterns
If there are many objects of the same type, the object URL should contains the id of the object.
If this object is a singleton object of that type, the id is not needed.http://www.xyz.com/library/books/668102
http://www.xyz.com/library
Get the object representation
HTTP GET is used to obtain a representation of the object, which has optional extension convention to indicate which format is needed. HTTP header "Accept" is also used to indicate the expected content format. Note also that the representation of the whole object is returned. There is no URL representation at the attribute level.
GET /library/books/668102.json HTTP/1.1
Host: www.xyz.com
Accept: application/json
Modify an existing Object
HTTP PUT is used to modify the object, the request body contains the representation of the Object after successful modification.Create a new Object
HTTP PUT is also used to create the object if the caller has complete control of assigning the object id, the request body contains the representation of the Object after successful creation.
PUT /library/books/668102 HTTP/1.1
Host: www.xyz.com
Content-Type: application/xml
Content-Length: nnn
<book>
<title>Restful design</title>
<author>Ricky</author>
</book>
If the caller has no control in the object id, HTTP POST is made to the object's parent container with the request body contains the representation of the Object. The response body should contain a reference to the URL of the created object.
POST /library/books HTTP/1.1
Host: www.xyz.com
Content-Type: application/xml
Content-Length: nnn
<book>
<title>Restful design</title>
<author>Ricky</author>
</book>
HTTP/1.1 301 Moved Permanently
Content-Type: application/xml
Location: /library/books/668102
<ref>http://www.xyz.com/library/books/668102</ref>
Call a method of the Object
HTTP POST is used to invoke a method of the object, the method is indicated in a mandated parameter "action". The arguments of the method can also be encoded in the URL (for primitive types) or in the request body (for complex types)
POST /library/books/668102?action=buy&user=ricky HTTP/1.1
Host: www.xyz.com
POST /library/books/668102?action=buy HTTP/1.1
Host: www.xyz.com
Content-Type: application/xml; charset=utf-8
Content-Length: nnn
<user>
<id>ricky</id>
<addr>175, Westin St. CA 12345</addr>
</user>
Destroy an existing Object
HTTP DELETE is used to destroy the object. This release all the resources associated with this object.DELETE /library/books/668102 HTTP/1.1
Host: www.xyz.com
Container Patterns
The immediate parent of a container must be an object (can be a singleton object without an id or an object with an id). Container "contains" other objects or containers. If a container is destroyed, everything underneath will be destroyed automatically in a recursive manner.
http://www.xyz.com/library/books
http://www.xyz.com/library/dvds
http://www.xyz.com/library/books/668102/chapters
In
GET operation, by default the container only return the URL reference
of its immediate children. An optional parameter "expand" can be used
to request the actual representation of all children and descendants.
A more sophisticated GET operation can contain a "criteria" parameter to show only the children that fulfills certain criteria.
GET http://www.xyz.com/library/book?query=(author='ricky')
Reference Patterns
Long running transactions
Concurrent Updates (ETags)
Opinions expressed by DZone contributors are their own.
Comments