In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the knowledge points of RestFul API". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. important concepts
REST, the abbreviation of REpresentational State Transfer. The translation of this phrase is "state transformation at the expressive level". In this way, it is very obscure to understand. In fact, the full name of REST is Resource Representational State Transfe, which translates bluntly to "state transfer" of "resources" in some form of "expression" in network transmission. If you still can't continue to understand, please read on. I'm sure the following explanation will help you understand what REST is.
We interpret the concepts mentioned above separately in order to gain a better understanding, but in fact you don't need to understand the following concepts to understand what I'm going to introduce in the next section. However, in order to better talk about "RestFul API" with others, I suggest you still have a good understanding!
Resource: we can refer to real object data as resources. A resource can be either a collection or an individual. For example, our class classs represents a collection of resources, while a specific class represents a single individual resource. Each resource has a specific URI (uniform Resource Locator) corresponding to it. If we need to get this resource, we can access the URI, for example, to get a specific class: / class/12. In addition, resources can also contain child resources, such as / classs/classId/teachers: list the information of all teachers in a specified class
Form of expression (Representational): "resource" is an information entity, which can have a variety of external manifestations. We refer to the specific form of "resource" such as json,xml,image,txt and so on as its "presentation layer / presentation".
State transfer (State Transfer): you must be confused when you first see this word? Inner BB: what is this Nima? In vernacular, the state transfer in REST is more about the state of server-side resources, for example, you change the state of resources by adding, deleting, changing and querying (through HTTP verbs). Ps: Internet communication protocol HTTP protocol, is a stateless protocol, all resource states are stored on the server side.
Based on the above explanation, let's summarize what the RESTful architecture is:
Each URI represents a resource
Transfer some form of this resource, such as json,xml,image,txt, etc., between the client and the server.
The client operates on the server-side resources through specific HTTP verbs to achieve the "state transformation of the presentation layer".
II. REST interface specification
1. Action
GET: request to get specific resources from the server. For example: GET / classs (get all classes)
POST: create a new resource on the server. For example: POST / classs (create a class)
PUT: update the resource on the server (the client provides the entire updated resource). For example: PUT / classs/12 (update class number 12)
DELETE: removes a specific resource from the server. For example: DELETE / classs/12 (delete class number 12)
PATCH: update the resources on the server (the client provides changed properties, which can be seen as partial updates), which are used less, so I won't give an example here.
2. Path (API naming)
The path, also known as "endpoint", represents the specific URL of the API. The common specifications in actual development are as follows:
There can be no verbs in the URL, only nouns, and nouns in API should also be used in the plural. Because the resources in REST often correspond to the tables in the database, and the tables in the database are all "collection" of the same record. If the API call does not involve resources (such as computation, translation, etc.), you can use verbs. For example: GET / calculate?param1=11 m2o33
Do not use capital letters, it is recommended not to use the middle bar-no lower bar _ such as the invitation code is written as invitation-code instead of invitation_code
Talk is cheap! Let's give a practical example to illustrate it. If there is such an API that provides information about the class, as well as the students and teachers in the class, its path should be designed as follows.
The interface uses nouns as much as possible, but verbs are forbidden. Here are some examples:
GET / classs: list all classes POST / classs: create a new class GET / classs/classId: get information about a specified class PUT / classs/classId: update information about a specified class (generally tend to update as a whole) PATCH / classs/classId: update information about a specified class (generally partial updates) DELETE / classs/classId: delete a class GET / classs/classId/teachers : list all teachers in a specified class GET / classs/classId/students: list all students in a specified class DELETE classs/classId/teachers/ID: delete the information of a specified teacher under a specified class
Counterexample:
/ getAllclasss / createNewclass / deleteAllActiveclasss
Sort out the hierarchical structure of resources, for example, the scope of the business is the school, then the school will be the primary resource: / schools, teacher: / schools/teachers, student: / schools/students is the secondary resource.
3. Filter information (Filtering)
If we need to add specific conditions when querying, it is recommended to use the form of the url parameter. For example, we want to query classes whose state status is active and name is guidegege:
GET / classs?state=active&name=guidegege
For example, we need to implement a paged query:
GET / classs?page=1&size=10 / / specify page 1 with 10 data per page
4. Status code (Status Codes)
III. HATEOAS
The ultimate point of RestFul is hateoas, but this is rarely used in real projects.
The above is the most basic thing of RESTful API, and it is also the easiest thing to practice in our usual development process. In fact, it's best for RESTful API to Hypermedia, that is, to provide links in the returned results to other API methods so that users know what to do next without looking up the document.
For example, when a user makes a request to the root of api.example.com, he or she will get such a document.
{"link": {"rel": "collection https://www.example.com/classs"," href ":" https://api.example.com/classs", "title": "List of classs", "type": "application/vnd.yourformat+json"}}
The above code indicates that there is a link attribute in the document, and the user will know what API to call next by reading this attribute. Rel represents the relationship between the API and the current URL (collection relationship, and gives the URL of the collection), href represents the path of the API, title represents the title of the API, and type indicates the design of the return type Hypermedia API is called HATEOAS.
There is an API library called HATEOAS in Spring, which makes it easier to create an API that conforms to the HATEOAS design.
This is the end of the content of "what are the knowledge points of RestFul API". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.