Over a million developers have joined DZone.

RESTFul Design Patterns

DZone 's Guide to

RESTFul Design Patterns

· Java Zone ·
Free Resource

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


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

<title>Restful design</title>

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

<title>Restful design</title>
HTTP/1.1 301 Moved Permanently
Content-Type: application/xml
Location: /library/books/668102


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

<addr>175, Westin St. CA 12345</addr>

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.

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)

From  http://horicky.blogspot.com/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}