Some new experiences of RESTful Web service

Source: Internet
Author: User
Tags representational state transfer

Rest-representational State Transfer

Actually, this representational refers to resource's representation.

Resource is actually an entity, and all objects in rest are considered resource. Each Resource has a URI (Unique Resource Identifier) that accesses the URI to obtain the corresponding Resource

Representational refers to the presentation form of resource, which can be txt, JSON, HTML, XML, binary, JPG, png .....

The state transfer occurs during client and server interaction. Because HTTP is stateless, all state is stored in the server, and if the client wants to operate the server, it needs to have the server state transfer. This transfer occurs in the presentation layer (representation) and is therefore called rest. In order to think that the state transfer,client need to do the crud (Create, Read, Update, delete) operations via HTTP methods:post, GET, PUT, delete respectively. Where post can be used either to create a new resource or to update a resource


Also asked in the interview what makes RESTful Web service actually feels like to ask some of the conditions and principles of restful

1. Resource all correspond to a unique URI, or Resource-driven. URIs make up a global namespace

2. Using HTTP methods

3. Stateless stateless, the request to the server stateless, which makes the server's transformation is not visible to the client, because in a continuous request, the client does not rely on the same server

4. A variety of resource format, can be TXT, JSON, HTML, XML and so on.


Because the URI indicates a resource resource so the HTTP verb should not appear in the URI, only the noun that represents resource

HTTP methods:

Get (Select)-Get resources from the server

POST (create/update)-Create a new resource or update a resource on the server

PUT (update)-the full resource after the server update client provides a change

Delete-Deletes a resource on the server

PATCH (update)-Updates the client to provide changed properties on the server

Head-Get the head of the resource

OPTIONS-Get information about which properties of a resource are available to the client


Status Codes

Common to have these:

$ OK-[GET] The server successfully returns the data requested by the user

201 CREATED-[post/put/patch] user created or modified data successfully

204 NO CONTENT-[delete] user deleted data successfully

INVALID request-[Post/put/patch] The user has failed to make a new or modified operation with the wrong server

404 Not FOUND-[*] User-issued request is a record that does not exist

INTERNAL Server Error-[*] The server is wrong and the user cannot determine whether the request was made successfully

After an error occurs, the server returns an error message to the user

All the Code:http://en.wikipedia.org/wiki/list_of_http_status_codes


In fact, because rest uses HTTP methods is also based on HTTP protocol, so you can also use the HTTP servlet to implement rest because HttpServlet also defines doget, DoPost, DoPut, Dodelete These methods so you can rewrite these methods to implement rest.


In the process of review I also refer to the Http://www.ruanyifeng.com/blog/2014/05/restful_api.html blog post feel good to write ~


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.