REST and the Missing Link

DZone 's Guide to

REST and the Missing Link

· Java Zone ·
Free Resource

This morning I was thinking some more about the topic in the previous post, and was also doing some refactoring of our client code to be more RESTish. I had some cases where previously I had been thinking along the "usual" way of doing things, and changing it to be more strict REST helped out a lot. In particular, I tried to find the missing links.

I'll give you an example. We have a lot of CRUDish operations that need to be done. In particular, we have selection lists, where a list of references to already created entities need to be maintained. In the old version I would first do a query for a list of possible entities that could be added to a list, which would return {id,name} tuples, that the user could select from. Then the client would create a link and post/put it to the server. Simple enough, but what if the REST API changes? Then the client would be trying to do something which is not allowed. Something was wrong.

The solution was to simply follow the REST constraints. The client would first query the resource itself that represents the list, then in that response see that a link to a query for "possibleEntities" (or somesuch) was available, and enable it in the UI. When the user clicks on the "Add" button/item in the UI the client would then perform the query. The client doesn't "know" how to construct this query, because all it has to do is do a GET on the provided link, whatever it is. The result would be a list of links, each with a name and a href (like HTML links, but encoded in JSON). The client would present the list, and the user could select from it. Once selected the client then simply takes the link and do a post or put on it. It doesn't know how to create the link itself, and doesn't have to.

This gives me as the REST API developer freedom to change how that link is constructed at any time, without impacting the client. Maybe it was "addstuff?id=123" or "/somelist/123" or something else, but as long as the client only knows that "posting to this link will add the item to the list", it's fine. I don't have to worry about "backwards compatibility" in terms of URL construction. Only if the link relationships change will there be compatibility issues.

And that's what I'm starting to get about properly doing REST, with hypertext as the driver of my client. It's all in the links. And that is why soooo many who claim they're doing REST really aren't, like the CouchDB example from the last post. They are missing the links. And by extension, they are missing the point about REST.


From http://www.jroller.com/rickard/entry/rest_and_the_missing_link


Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}