I am working on a project whose current stable version is written using JSP/Servlets and it needs to be re-written as Single Page Application using client side MVC framework Backbone.js and REST services on server written using spring web MVC. Same set of REST services should be used for iOS application written using Objective C.
This is the first time I am writing non-trivial REST services and I have gone through a book on Rest and many blog entries as well as many StackOverflow entries like following:
- Non-CRUD operations in a RESTful service
- Best practice for partial updates in a RESTful service
- REST practices for validating a resource
- http://abstractfinal.blogspot.in/2007/04/restful-urls-for-non-crud-operations.html
- http://www.infoq.com/articles/designing-restful-http-apps-roth
I have not yet found any definite answer to all of my questions which I encountered during design of the REST services. I have done design of those services but I want to validate my design.
My application has Car as a main resource. So my URI to get all cars becomes something like
GET http:/host:port/mywebappname/cars
URI to get a particular car is
GET http:/host:port/mywebappname/cars/MH12-1234
where
MH12-1234
is id of the car.My trouble begins when I want to get all such cars those are manufactured by Ford and Hyundai in 2010. Probable URI in my opinion becomes either
GET http:/host:port/mywebappname/cars/search/Ford;Hyundai/2010
or
GET http:/host:port/mywebappname/car/search?make=Ford,Hyundai&year=2010
- Which one of these is right?
- I could not imagine "search" as resource so I am keeping car as resource and search as a operation on it. Is this against REST ideology?
My car resource has many possible operations, e.g. painting a car, washing a car etc. URIs of those operations look like following to me.
POST http:/host:port/mywebappname/cars/MH12-1234/paint/yellow/glossy
or
POST http:/host:port/mywebappname/cars/MH12-1234/paint?color=yellow&finish=glossy
- Which one of these is right? and should I send entire car object as a part of request body?
- Some people suggest that operation name should be sent as custom attribute of request header. So in my case
paint
will go inside request header. In such case, where do I send information likecolor=yellow
andfinish=glossy
? As a part of request body or header?
Once client reads the car resource, it may want to change its wheel to some other suitable wheel. So URI for this operation may look like
GET http:/host:port/mywebappname/car/MH12-1234/replace/wheel/rear-left
This is basically replacing rear left wheel with some other suitable wheel.
- Is this right approach?
- Should this service return entire car or just the new wheel?
User who is playing with the car object in the client interface may try various operations on the car. E.g. user may paint it yellow and then paint it green and replace multiple wheels and then ultimately click on save. For each action that user initiated I have to post the entire car object or part of it to the server. So is it OK to send the part of it to avoid package size on the wire?
Now those request are sent as either PUT or POST and server carries out those operations in memory and returns modified car object. Now car resource is getting sent as request and car object is received as response. Is this a valid scenario?
If yes, then in both cases (save button click or any operation) I have to POST or PUT the same car resource to the server. I want to actually save car resource to database when save button is clicked and in case of other operations I just want to carry out the operation in server memory and return modified resource. How do I differentiate on the server about the user clicking on save as in both cases I PUT or POST the resource?