Wuaner Wuaner - 2 months ago 7
reST (reStructuredText) Question

URI of REST to reflect relationship of resources?

I knew some name conversions of REST API, for example resource name should be plural, using different HTTP method with same URI to perform different action on that resource, etc.

But as URI should reflect relationship of resources, I am a little confused. Take SO as a example, when update a existed comment of a answer, URI should looks like:

PUT /{contextPath}/questions/{questionId}/answers/{answerId}/comments/{commentId}


But I feel awkward when using this
so-called standard
URI because:


  1. It's a little verbose, especially when the hierarchical is very
    deep.

  2. questionId and answerId is completely unnecessary here, since
    commentId is sufficient for server to identify a comment record.



So what's the appropriate way to deal with this? should I always follow name conversions, or make some changes when the relationship hierarchical of resources is very deep?

Answer

I emphatically disagree that "URI should reflect relationship of resources".

URIs are pointers to resources. That's it. There are conventions for making them human-readable, and therefore easier to work with. There is certainly no hard-and-fast rule that relationships should be modeled on the URI path. Feel free to model resources in a flat, rather than hierarchical manner. Use links to model relationships between the resources, and query parameters to narrow down collections.

Comments