5

I've been struggling with understanding a few points I keep reading regarding RESTful services. I'm hoping someone can help clarify.

1a) There seems to be a general aversion to generated code when talking about RESTful services.

1b) The argument that if you use a WADL to generate a client for a RESTful service, when the service changes - so does your client code.

Why I don't get it: Whether you are referencing a WADL and using generated code or you have manually extracted data from a RESTful response and mapped them to your UI (or whatever you're doing with them) if something changes in the underlying service it seems just as likely that the code will break in both cases. For instance, if the data returned changes from FirstName and LastName to FullName, in both instances you will have to update your code to grab the new field and perhaps handle it differently.

2) The argument that RESTful services don't need a WADL because the return types should be well-known MIME types and you should already know how to handle them.

Why I don't get it: Is the expectation that for every "type" of data a service returns there will be a unique MIME type in existence? If this is the case, does that mean the consumer of the RESTful services is expected to read the RFC to determine the structure of the returned data, how to use each field, etc.?

I've done a lot of reading to try to figure this out for myself so I hope someone can provide concrete examples and real-world scenarios.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Josh M.
  • 23,573
  • 23
  • 96
  • 160
  • This article does a good job of summing up my questions but from the other point of view and I don't think it is explained well enough so really I'm trying to clarify the major points in this article: http://bitworking.org/news/193/Do-we-need-WADL – Josh M. Mar 02 '11 at 22:59

2 Answers2

4

REST can be very subtle. I've also done lots of reading on it and every once in a while I went back and read Chapter 5 of Fielding's dissertation, each time finding more insight. It was as clear as mud the first time (all though some things made sense) but only got better once I tried to apply the principles and used the building blocks.

So, based on my current understanding let's give it a go:

Why do RESTafarians not like code generation?

The short answer: If you make use of hypermedia (+links) There is no need.

Context: Explicitly defining a contract (WADL) between client and server does not reduce coupling enough: If you change the server the client breaks and you need to regenerate the code. (IMHO even automating it is just a patch to the underlying coupling issue).

REST helps you to decouple on different levels. Hypermedia discoverability is one of the goods ones to start with. See also the related concept HATEOAS

We let the client “discover” what can be done from the resource we are operating on instead of defining a contract before. We load the resource, check for “named links” and then follow those links or fill in forms (or links to forms) to update the resource. The server acts as a guide to the client via the options it proposes based on state. (Think business process / workflow / behavior). If we use a contract we need to know this "out of band" information and update the contract on change.

If we use hypermedia with links there is no need to have “separate contract”. Everything is included within the hypermedia – why design a separate document? Even URI templates are out of band information but if kept simple can work like Amazon S3.

Yes, we still need a common ground to stand on when transferring representations (hypermedia), so we define your own media types or use widely accepted ones such as Atom or Micro-formats. Thus, with the constraints of basic building blocks (link + forms + data - hypermedia) we reduce coupling by keeping out of band information to a minimum.

As first it seems that going for hypermedia does not change the impact of change :) : But, there are subtle differences. For one, if I have a WADL I need to update another doc and deploy/distribute. Using pure hypermedia there is no impact since it's embedded. (Imagine changes rippling through a complex interweave of systems). As per your example having FirstName + LastName and adding FullName does not really impact the clients, but removing First+Last and replacing with FullName does even in hypermedia.

As a side note: The REST uniform interface (verb constraints - GET, PUT, POST, DELETE + other verbs) decouples implementation from services.

Maybe I'm totally wrong but another possibility might be a “psychological kick back” to code generation: WADL makes one think of the WSDL(contract) part in “traditional web services (WSDL+SOAP)” / RPC which goes against REST. In REST state is transferred via hypermedia and not RPC which are method calls to update state on the server.

Disclaimer: I've not completed the referenced article in detail but I does give some great points.

Derick Schoonbee
  • 2,793
  • 1
  • 20
  • 36
  • thanks for your answer. I appreciate you taking the time to attempt to explain. While it is still murky to me regarding machine-to-machine communication and how REST would be better than SOAP in that regard, I do understand the benefit of using known MIME types and in some cases, the benefit of using REST over SOAP. I believe your answer makes a better attempt at answering my question so I'll award you the bounty. Although I do still feel like the complete picture hasn't been painted, for me. Thanks. – Josh M. Mar 16 '11 at 11:33
  • is it typical for a REST(ful) service to have an "index" page - much like the defailt page of a website? People say that the client should not need to know how to construct a URL because the client just follows the links in the REST response. So how does the client perform the first request, then? – Josh M. Mar 17 '11 at 16:31
  • You need an entry point the your service. It can be for "humans" (user agents) like a bookmark of a home page in html, or a url for non-human user agents. Then you do content-negotiation (what your browser does automatically), resolve links etc. You can not eliminate all out of band information, but one URI is quite close enough :) PS: It's good be picky... keeps to you honest on sticking to REST principles ;) – Derick Schoonbee Mar 19 '11 at 12:52
0

I have worked on API projects for quite a while. To answer your first question.

Yes, If the services return values change (Ex: First name and Last name becomes Full Name) your code might break. You will no longer get the first name and last name.

You have to understand that WADL is a Agreement. If it has to change, then the client needs to be notified. To avoid breaking the client code, we release a new version of the API.

The version 1.0 will have First Name and last name without breaking your code. We will release 1.1 version which will have the change to Full name.

So the answer in short, WADL is there to stay. As long as you use that version of the API. Your code will not break. If you want to get full name, then you have to move to the new versions. With lot of code generation plugins in the technology market, generating the code should not be a issue.

To answer your next question of why not WADL and how you get to know the mime types.

WADL is for code generation and serves as a contract. With that you can use JAXB or any mapping framework to convert the JSON string to generated bean objects.

If not WADL, you don't need to inspect every element to determine the type. You can easily do this.

var obj = jQuery.parseJSON('{"name":"John"}'); alert( obj.name === "John" );

Let me know, If you have any questions.

  • Thanks for your answer. But I don't feel you truly answered what I was trying to get at (sorry if it was unclear). For question 1 I was pointing out that there is a general "don't do it" feeling from the REST community toward both code generation and WADL - why is that? For question 2 I was pointing out that without a WADL there is no contract so you can't "expect" to receive the same structure with each call to the same endpoint. If you query person 1 you may get FirstName while person 2 returns FirstName and Lastname. Hopefully that makes sense. – Josh M. Mar 10 '11 at 03:20
  • Okay. Why not WADL? Its because WADL is verbose like WSDL. In the HTTP world its all about status codes. I can explain my endpoints and return codes for scenarios in a HTML file. If I can look at at JSON in a browser, I can code to it. If the endpoint(or API provider ) changes the response, then the provider of the API will change the version of the API. The provider cannot change the response structure at will. If the first call gets full name, then first name and last name will be null. thats true representation of JSON. – Vanchinathan Chandrasekaran Mar 10 '11 at 03:31