What are REST resources?

后端 未结 11 1193
青春惊慌失措
青春惊慌失措 2020-11-29 20:53

What are REST resources and how do they relate to resource names and resource representations?

I read a few articles on the subject, but they were too abstract and t

11条回答
  •  情书的邮戳
    2020-11-29 21:45

    The URL is never a resource or its name or its representation.

    URL just tells where the resource is located and You can invoke GET,POST,PUT,DELETE etc on this URL to invoke the resource.

    Data responded back are the resources while the form of the data is its representation.

    Let's say Your URL with given GET parameters can output a JSON resource - this is the JSON representation of this resource. While with other flag in the GET it could respond with the same data in XML - that will be another representation of the very same resource.

    EDIT: Due to the comments to the OP and to my answer I'm adding another explanations.

    Also the resource name is considered to be the 'script name', e.g. in this case it is users.json while this resource name is self describing the resource representation itself - when calling this resource we expect the resource is in JSON, while when calling e.g. users.xml we would expect the data in XML.

    1. When I change the offset parameter in GET the response contains different data set - is it a new resource or its representation?
    2. When I define which columns are returned in response in GET, is it a different resource or different representation, or?
    1. Well, here the problem and answer are clear - we still call the same URL, the server responses with the data in the same form (still it is JSON), data still contains information about users - just the information itself has changed due to the new offset parameter. So it is obvious that it is still the same resource with the same representation and the same resource name as before.
    2. Second problem could be a little confusing. Though we are calling the same resource, though the resource contains the same data (just with only predefined column set) and though the data is in the same representation it could seem to us as a different resource. But due to the points in the paragraph above it is nor the different resource or different representation. Though the data set contains less information the requesting side (filtering this data set) should be considering this and behave accordingly. So again: it is the same resource with the same resource name and the same resource representation.

提交回复
热议问题