当前位置:网站首页>2021.9.29 learning log restful architecture

2021.9.29 learning log restful architecture

2022-06-13 05:39:00 kaesarsk

restful framework

CSDN Reference resources
restful Is a constraint of Architecture , A constraint criterion is given .
REST:Representational State Transfer( The transformation of the state of the image layer ):

1. every last URI Represents a resource ;
2. Between client and server , A layer of expression that conveys this resource ;
3. Client through four HTTP Verb (get、post、put、delete), Operate on the server-side resources , Realization ” Represent layer state transformation ”.

REST Architecturally 6 Great principles

1. C-S framework 
	 The data is stored in Server End ,Client Just use the end . The advantage of a complete separation of the two ends makes client The portability of the end code becomes stronger ,
Server The expansion of the end becomes stronger . Both ends are developed separately , Mutual interference .
2.  No state 
	http The request itself is stateless , be based on C-S framework , Every request from the client has enough information to be recognized by the server .
 Some of the information required for the request is contained in URL The query parameters of 、header、body, The server can request various parameters according to , No need to save the state of the client ,
 Return the response to the client correctly . Stateless features greatly improve the robustness and scalability of the server .
	 Of course, this total stateless constraint also has disadvantages , Each request from the client must carry the same repeated information to determine its identity and status ( This is also necessary ),
 Cause redundancy of transmission data , But this disadvantage is important for performance and use , Almost ignored .
3. Unified interface 
	 That's it REST The heart of the architecture , A unified interface for RESTful Service is very important . The client only needs to pay attention to the implementation interface , The readability of the interface is enhanced , It is convenient for users to call .
4. Consistent data format 
	 The data format returned by the server is either XML, Or Json( get data ), Or directly return the status code 
	 Self describing information , Each data should be self describing , It is convenient for the code to process and parse the content .
 Such as through HTTP The returned data contains  [MIME type ] Information , We from MIME type You can know the specific format of the data , It's a picture. , Video or JSON,
 Client pass body Content 、 Query string parameters 、 Request the head and URI( Resource name ) To transmit state . Server through body Content , The response code and response header are sent to the client .
 This technology is called hypermedia ( Or hypertext links ).
	 In addition to the above ,HATEOS Also means that , Links can also be included in the returned body( Or the head ) in , In order to provide URI To retrieve the object itself or the associated object .
 If you ask for a tweet , The server response information should include other information related to this Weibo URL, The client can further utilize these URL Initiate a request for information of interest , Another example is that paging can get the next page's data from the return data of the first page URT It's also based on this principle .

	JSON For example :
code—— Containing an integer type HTTP Response status code .
status—— Include text :”success”,”fail” or ”error”.HTTP The status response code is 500-599 Between for ”fail”, stay 400-499 Between for ”error”,
	 Others are ”success”( for example : The response status code is 1XX、2XX and 3XX). According to the actual situation, this is not necessary .
message—— When the state value is ”fail” and ”error” Effective when , Used to display error messages . Refer to internationalization (il8n) standard ,
	 It can contain information numbers or codes , Can contain only one of , Or include and separate with separator at the same time .
data—— Containing the response body. When the state value is ”fail” or ”error” when ,data Contains only the error cause or exception name 、 perhaps null It's OK, too 

 Return a successful response JSON:
{
  "code": 200,
  "message": "success",
  "data": {
    "userName": "123456",
    "age": 16,
    "address": "beijing"
  }
}
 Return the failed response json Format :
{
  "code": 401,
  "message": "error  message",
  "data": null
}

4. System layering 
	 The client is usually unable to indicate whether it is directly or indirectly connected to the end server , The security policy should also be considered when layering 
5. Caching 
	 On the World Wide Web , The client can cache the response content of the page . So the response should be implicitly or explicitly defined as cacheable ,
 If it is not cacheable, the client should avoid using old data or dirty data to respond after multiple requests .
 Properly managed cache will partially or completely remove the interaction between client and server , Further improve performance and ductility .
6. On demand coding 、 Customizable code ( Optional )
	 The server can choose to temporarily send some function codes to the client for execution , To customize and extend some functions of the client .
 For example, the server can return some  Javascript  The code lets the client execute , To achieve certain functions .

 Tips :REST Design criteria in Architecture , Only on-demand coding is optional . If a service violates any other criterion , Strictly speaking, it can't be called RESTful style .
原网站

版权声明
本文为[kaesarsk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130537201815.html