News: RESTful Web Service Discoverability, part 4

  1. RESTful Web Service Discoverability, part 4 (4 messages)

    This is the forth of a series of articles about setting up a secure RESTful Web Service using Spring 3.1 and Spring Security 3.1 with Java based configuration. The article will focus on Discoverability of the REST API, HATEOAS and practical scenarios driven by tests.

    The REST with Spring series:

    Introducing REST Discoverability

    Discoverability of an API is a topic that doesn’t get enough well deserved attention, and as a consequence very few APIs get it right. It is also something that, if done right, can make the API not only RESTful and usable but also elegant.

    To understand discoverability, one needs to understand that constraint that is Hypermedia As The Engine Of Application State (HATEOAS); this constraint of a RESTful API is about full discoverability of actions/transitions on a Resource from Hypermedia (Hypertext really), as the only driver of application state. If interaction is to be driven by the API through the conversation itself, concretely via Hypertext, then there can be no documentation, as that would coerce the client to make assumptions that are in fact outside of the context of the API.

    Also, continuing this logical train of thought, the only way an API can indeed be considered RESTful is if it is fully discoverable from the root and with no prior knowledge – meaning the client should be able to navigate the API by doing a GET on the root. Moving forward, all state changes are driven by the client using the available and discoverable transitions that the REST API provides in representations (hence Representational State Transfer).

    In conclusion, the server should be descriptive enough to instruct the client how to use the API via Hypertext only, which, in the case of a HTTP conversation, may be the Link header.

    Concrete Discoverability Scenarios (Driven by tests)

    So what does it mean for a REST service to be discoverable? Throughout this section, we will test individual traits of discoverability using Junit, rest-assured and Hamcrest. Since the REST Service has been secured in part 3 of the series, each tests need to first authenticate before consuming the API. Some utilities for parsing the Link header of the response are also necessary.

    Discover the valid HTTP methods

    When a RESTful Web Service is consumed with an invalid HTTP method, the response should be a 405 METHOD NOT ALLOWED; in addition, it should also help the client discover the valid HTTP methods that are allowed for that particular Resource, using the Allow HTTP Header in the response:

    public void
       // Given
       final String uriOfExistingResource = this.restTemplate.createResource();
       // When
       Response res = this.givenAuthenticated().post( uriOfExistingResource );
       // Then
       String allowHeader = res.getHeader( HttpHeaders.ALLOW );
       assertThat( allowHeader, AnyOf.<String> anyOf(
        containsString("GET"), containsString("PUT"), containsString("DELETE") ) );

    Discover the URI of newly created Resource

    The operation of creating a new Resource should always include the URI of the newly created resource in the response, using the Location HTTP Header. If the client does a GET on that URI, the resource should be available:

    public void whenResourceIsCreated_thenURIOfTheNewlyCreatedResourceIsDiscoverable(){
       // When
       Foo unpersistedResource = new Foo( randomAlphabetic( 6 ) );
       Response createResponse = this.givenAuthenticated().contentType( MIME_JSON )
        .body( unpersistedResource ).post( this.paths.getFooURL() );
       final String uriOfNewlyCreatedResource = createResp
        .getHeader( HttpHeaders.LOCATION );
       // Then
       Response response = this.givenAuthenticated()
        .header( HttpHeaders.ACCEPT, MIME_JSON ).get( uriOfNewlyCreatedResource );
       Foo resourceFromServer = response.body().as( Foo.class );
       assertThat( unpersistedResource, equalTo( resourceFromServer ) );

    The test follows a simple scenario: a new Foo resource is created and the HTTP response is used to discover the URI where the Resource is now accessible. The tests then goes one step further and does a GET on that URI to retrieve the resource and compares it to the original, to make sure that it has been correctly persisted.

    Read the rest of the article

    Threaded Messages (4)

  2. forth/fourth[ Go to top ]

    This is the forth of a series of articles

    I guess you mean this is the fourth of a series of articles...

  3. forth/fourth[ Go to top ]

    I do mean fourth, sorry about the typo. The original has been corrected, no way to fix it here unfortunatelly.

  4. "When we GET a particular Foo instance, we should be able to discover what we can do next: we can list all the available Foo resources"

    Really? So if I were to load an Event from, say facebook, that should contain links to all the Events in Facebook??? Standby for 3 zillion link elements!

    There are 2 sensible approaches to discovery:

    1. Search: given some critieria find resources that match
    2. Navigate a hierarchy: for a given resource find its children. What constitutes "children" is subjective and varies from application to application

    Both of these are well catered for by Webdav. No extra weirdness required!

  5. Hi, thanks for the suggestion. Here is my 2 cents on it - while WebDAV is a useful protocol, I don't see it fully solving the problems that addresses; here is an good article on why: http://www.mnot.net/blog/2004/04/27/webdav4rest

    What's more, REST is becoming fairly standard, so I don't think `weirdness` applies - if anything, I would find it wierd (not in a bad way) to see a WebDAV based API out in the wild.

    Thanks for the feedback.