What is? REST？
Six design specifications define a REST Characteristics of the system :
- client - The server : Isolation between client and server , Server provides services , Client consumption .
- No state : Every request from the client to the server must contain the information necessary to understand the request . let me put it another way , The server will not store the information requested by the client for the next use .
- Caching : The server must indicate whether client requests can be cached .
- Layered system : The communication between client and server should be in a standard way , When the middle tier responds instead of the server , The client doesn't need to make any changes .
- Unified interface : The communication method between server and client must be unified .
- On demand coding : The server can provide executable code or scripts , For clients to execute in their environment . This constraint is the only one that is optional .
What is a RESTful Of web service？
REST The original purpose of architecture is to adapt to the world wide web HTTP agreement .
RESTful web services The core of the concept is “ resources ”. Resources can be used URI To express . Client side usage HTTP Protocol defined methods to send requests to these URIs, Of course, it may lead to these being visited ” resources “ A change of state .
HTTP The standard methods are as follows :
========== ===================== ================================== HTTP Method Behavior Example ========== ===================== ================================== GET Get information about resources http://example.com/api/orders GET Get information about a particular resource http://example.com/api/orders/123 POST Create new resources http://example.com/api/orders PUT Update resources http://example.com/api/orders/123 DELETE Delete resources http://example.com/api/orders/123 ========== ====================== ==================================
REST Design doesn't require a specific data format . In the request, the data can be JSON form , Or sometimes as url Query parameter items in .
Design a simple web service
insist REST Design a web service perhaps API The task becomes an exercise of identifying resources to be displayed and how they are affected by different request methods .
for instance , We're going to write a to-do application and we want to design one for it web service. The first thing to do is to decide what kind of root to use URL To access the service . for example , We can visit through this :http://[hostname]/todo/api/v1.0/
Here I've decided to URL Contains the name of the app and API Version number of . stay URL The inclusion of application names in helps to provide a namespace that distinguishes other services on the same system . stay URL Including the version number in can help with future updates , If there are new and potentially incompatible features in the new version , It doesn't affect applications that rely on older features .
The next step is to choose which one will be exposed by the service ( Exhibition ) Resources for . This is a very simple application , We have a mission , So the only resource in our to-do list is the task .
Our mission resources are going to use HTTP The method is as follows :
========== =============================================== ============================= HTTP Method URL action ========== =============================================== ============================== GET http://[hostname]/todo/api/v1.0/tasks Retrieve the task list GET http://[hostname]/todo/api/v1.0/tasks/[task_id] Retrieve a task POST http://[hostname]/todo/api/v1.0/tasks Create a new task PUT http://[hostname]/todo/api/v1.0/tasks/[task_id] Update task DELETE http://[hostname]/todo/api/v1.0/tasks/[task_id] Delete task ========== ================================================ =============================
The tasks we define have the following properties :
- id: Unique identifier of the task . Numeric type .
- title: A brief description of the task . String type .
- description: Specific task description . Text type .
- done: The state of mission completion . Boolean value .
So far about us web service The design is basically completed . The rest is to achieve it ！