Rest – Do RESTful web services support both contract-first and contract-last approaches

contract-firstrestweb services

Does a RESTful web service (e.g. in a JAX-RS implementation) support both contract-first (top-down) approach and contract-last (bottom-up) approach?

Best Answer

Do RESTful web services support both contract-first and contract-last approaches?

It depends on what tools/frameworks you use.

What you are talking about applies to a SOAP web service and its accompanying WSDL.

A WSDL describes what a web service expects as input and what should a client expect as output. It defines the contract to be followed in order for both parties to communicate with each other. You can obtain the WSDL by doing contract-first or contract-last and you can later use this WSDL to generate code for a client stub or a service skeleton.

But doing REST isn't the same as doing SOAP. Processes that worked for SOAP (a protocol) can't necessarily be applied to REST (an architectural style) just because we are familiar with them.

Unlike SOAP that exposes methods and method signatures, REST exposes resources. An understanding of the media types used in the exchange of those resources is all a REST client needs in order to communicate with a REST web service. There is no need for a separate document to describe the resources.

Because of the HATEOAS principle, REST clients are more dynamic and can adapt to other services that communicate using the same media types. Exposing static service description documents would be limiting for a REST service.

Having said that, there are REST tools that do expose a description document, for example Jersey who exposes a WADL (contract-last). I'm sure you can use the published WADL to build client stubs and I don't see a reason why you couldn't write the WADL by hand (contract-first) and use it to generate stubs and skeletons. But as I said, that might not be the best solution for REST.

Here are some posts you might want to read to form an opinion if contract-last or contract-first approaches make sense in REST: