Merry Christmas and sorry for not posting in a few days. I was trying to meet the deadline for getting the updates for my book to the publisher and Geoff and I had to pull a couple of all nighters. The title of the book is Persistence in the Enterprise
and it should be released soon. I also just published an article on using the EJB 3 FP for WebSphere
I wanted to post on this a few days back. In the first installment of my Project Zero Series
(Part 2 based on Milestone 3 will be released shortly) we discuss RESTful patterns. One of the things that we covered is RESful patterns for getting a list of all items and a single record.
* http://host/resources/customer will return a list of customers.* http://host/resources/customer/roland will return the record for Roland.
One thing I have been getting asked is how do I RESTfully interact with a partial list. Not pagination of a list, but specifically asking for a list based on a set of keys. This is equivalent toselect * from table where key in (a,b,c,d,e)
. The most typical use case for this feature I have seen is a "Compare" feature in a shopping application, where I may select 3 or 4 items to compare side by side. By selecting a checkbox and clicking compare, I need to generate a RESTful URI with 3-4 keys. One thing to note that REST allows the use of query parameters as an alternative.
http://host/resources/customer/roland and http://host/resources/customer&customerId=Roland are both perfectly RESTful. I know that some people prefer not to use query parameters this way to access a single record. The rule of thumb I have heard is use the URI namespace for primary keys or other mandatory fields, and query parameters for filter criteria or optional parameters. With that in mind, there are three options for this RESTful queryOption 1: Namespace Option.
In this case, you add a set of keys after the product. This may require you to do some parsing of the URI, and it can be confusing if keys are Strings, because nested URI names can also imply relationships such as /category/3/product Option 2: Multiple Query parameters for key
Most servers will give you an array back.Option 3: Single comma separated key parameter.
This one seems more compact and straight forward, but again you have to parse yourself. However, if your service implementation is using a Select * from Product where productId IN(23,24,244,33), then you can pass this request parameter fairly simply into the In clause. Although with some implementations, building queries this way may by pass prepared statements.