What are REST resources?

2019-01-10 11:14发布

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 they left me more confused than I was before.

Is the following URL a resource? If it is, what is the name of that resource and what is its representation?

http://api.example.com/users.json?length=2&offset=5

The GET response of the URL should look something like:

[
   {
      id: 6,
      name: "John"
   },
   {
      id: 7,
      name: "Jane"
   }
]

11条回答
一夜七次
2楼-- · 2019-01-10 11:52

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

REST doesn't mean a great deal more then you use HTTP verbs (GET, POST, PUT, DELETE, etc) properly.

Is the following URL a resource?

All URLs are strings that tell computers where a resource can be located. (Hence the name: Uniform Resource Locator).

查看更多
姐就是有狂的资本
3楼-- · 2019-01-10 11:56

What is REST?

REST is an architecture Style which stands for Representational(RE) State(S) transfer(T).

What is REST Resource ?

Rest Resource is data on which we want to perform operation(s).So this data can be present in database as record(s) of table(s) or in any other form.This record has unique identifier with which it can be identified like id for Employee.

Now when this data is requested by unique url like http://www.example.com/employees/123,so ultimately data or record which is present in database will be converted to JSON/XML/Plain text format by Rest Service and will be sent to Consumer.

So basically,what is happening here is REPRESENTATIONAL STATE TRANSFER, in a way that state of the data present in database is transferred to another format which can be JSON/XML or plain text.

So in this case 1 employee represents 1 resource which can be accessed by unique url like http://www.example.com/employees/123

In case we want to get list of all resources(employees),we will do: http://www.example.com/employees

Hope this will help.

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-10 11:57

Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. REST-style architectures consist of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses. Requests and responses are built around the transfer of representations of resources. Resources are a set of addressable objects, basically files and documents, linked using URLs. As correctly pointed out above by Quentin, REST archiecture simply implies that you'd use the HTTP verbs GET/POST/PUT/DELETE...

查看更多
何必那么认真
5楼-- · 2019-01-10 12:02

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.
查看更多
聊天终结者
6楼-- · 2019-01-10 12:03

REST stands for REpresentational State Transfer. It's a method of transferring variable information from one location to another. A common method of doing this is using JSON - a way of formatting your variables so that they can be transferred without loss of information.

PHP, for example, has built in JSON support. Passing a PHP array to json_encode($array) will output a string in the format you posted (which by the way, is indeed a REST resource, because it gives you variables and information).

In PHP, the array you posted would turn out as:

Array (

    [0]=>Array (
        ['id']=>6;
        ['name']=>'John';
    )
    [1]=>Array (
        ['id']=>7;
        ['name']=>'Jane';
    )

)
查看更多
登录 后发表回答