What do you think of when you see an API? Is it an interface, a third party call, or an API document? At first glance, you might think this is pretty familiar. Isn’t this the everyday series of systems development? But when you think about it, the idea of an API is so vague in your mind. If you look at the API through a search engine, you’ll see something like this: API — Application Programming Interface, which is too abstract. Next, I’ll talk about the connections and differences between apis, REST apis, RESTful apis, and Web Services in a colloquial way, combining some of the lessons LEARNED during development.
API and REST API
What is the API? Here is the definition given by Wikipedia: Application Programming Interface (APIS; Application programming interface (API) is a contract that connects different components of a software system. This definition of AN API is too broad and abstract. In general terms, an API is a way (protocol) for one piece of application to “talk” to another. Kicked off in the Web application, the API is a main way for data retrieval through the network, the API documentation will be told that you retrieve data URL list, query parameters, request and response status, its purpose is to reduce the difficulty of Web application development, sharing of data between two applications (text, audio, video, images, etc.), While shielding the complex implementation details inside.
REST is short for Representational State Transfer, which literally means representation State Transfer. A REST API is a set of architectural rules, standards, or guidelines for how to build Web application apis, or a REST API is an architectural style that follows API principles. REST is specifically designed for Web applications to reduce the complexity of development and improve the scalability of the system. Here are some basic conditions and principles to follow when designing a REST-style system architecture:
- 1. In REST architecture, all things in the Web (text, audio, video, pictures, links) can be abstracted as resources in a unified way.
- 2. In the REST architecture, each resource has a unique resource identifier that does not change when the state of the resource changes
- 3. In REST architecture, all operations are stateless. REST architecture follows THE CRUD principle. All resources can be performed through the four behaviors of GET, POST, PUT, and DELETE.
- 4. Cacheable (optional), which is required in REST architectures to efficiently process large numbers of requests
- 5. Consistent interfaces
Now that you know the basic concepts of apis and REST apis, what are the similarities and differences between the two? In terms of the mathematical concept of set, API is a superset of REST API, and REST API is a subset of API. All REST apis are apis, but not all apis are REST apis. A more popular explanation is: All men are people, but not all men are men.
2. REST API and RESTful API
In the first section, you learned what the REST API is, and now we’ll talk about the similarities and differences between the REST API and the RESTful API. It’s easy for beginners to equate the two and think RESTful apis are REST apis. This may be taken literally, but when you get into the nature of the two, you’ll find it’s not. The REST API is a specification or guideline for Web API design, and the RESTful API is a concrete implementation of this architectural design principle or specification. In other words, RESTful apis are an informal way to implement REST apis. Because RESTful apis are one of many ways to implement REST apis, they do not fully meet all the REST API design principles, and each developer has a different focus when implementing a REST architecture.
It is easy for beginners to confuse the REST API with the RESTful API. I think they are just reading the words and not paying attention to the meaning (just like Chinese characters). It’s just like many people will equate a transgender person with a woman. A transgender person may look like a woman in appearance, but a transgender person can’t have children. It just satisfies most of the conditions that define a woman, but is not a woman in essence.
Next, deepen your understanding of the REST AND RESTful apis with a simple example. Here is an example of a RESTful API design that performs CURD operations:
A. resource B. resource C. student D. teacher d. book {id} represents a unique resource identifier.
The following is a specific small example, take student management as an example, design student management API. Student resources include ID, name and course information. Student resource information is as follows:
Now, we need to save the student data to the database, and then perform the operations of querying, modifying, and deleting the student data. The user of the Student Administration API calls the following API:
- 1, create student resources: [POST] www.example.com/student
- 2, access all student resources: [GET] www.example.com/students
- 3, for ID = 1001 student resources: [GET] www.example.com/student/100…
- 4, modify ID = 1001 student resources: [PUT] www.example.com/student/100…
- 5, DELETE the ID = 1001 student resources: [DELETE] www.example.com/student/100…
As mentioned earlier, the API shares data resources and hides internal implementations. The user (client) of the API is focused on the resource (reading the data) and does not need to know the internal structure of the API. The PROVIDER of the API (the server) is only concerned with its own internal implementation, not the state of the API consumer (the client). To further understand this concept, the following is an example of an internal implementation of the Student Administration API:
Description:
The sample code is implemented based on Spring MVC.
In addition to the above, you can also filter the query data by providing key-value pairs. For example, if you want to retrieve all student data and only want to retrieve female students, you can do this:
[GET] http://www.example.com/students?gender=female
Copy the code
Tip: If the API has data filtering, the server API implementation code needs to be adjusted.
In the previous section, we mentioned that RESTful apis are informal implementations or specifications of REST apis. Why do you say that? In RESTful API design, we can complete CURD operations by GET, create resources by DELETE behavior, and modify resources by POST behavior. The implementation method is not rigorous or does not strictly follow the constraints of REST API. So RESTful apis are informal implementations of REST apis.
3. REST and Web Service
3-1. What is a Web Service?
As described by the World Wide Web Consortium (W3C), Web Services provide a standard way to interoperate between different software applications running on various platforms and/or frameworks. Web Services are characterized by good interoperability and extensibility, as well as the use of XML to describe program processing. They can combine different services in a loosely coupled manner to implement complex operations. Programs that provide simple services can interact with each other to provide complex value-added services.
Two Web services communicate primarily over HTTP network protocols, known as SOA(Service Oriented Architecture), which rely on XML-RPC and Simple Object Access Protocol (SOAP).
Tip: Never confuse SOA (Service-oriented Architecture), which is a form of architectural design, with SOAP(Simple Object Access Protocol), which is a data exchange protocol.
A simple example: Suppose A Web Service A provides A function that allows other applications to access user information via A URL: [GET] www.abc.com/{id}. Id is the user’s… It doesn’t matter, as long as you can parse out the XML document returned by service A. In this way, the exchange of data between applications does not depend on specific languages and environments. It’s like two people from different countries and different languages can communicate as long as they know the grammatical structure of each other’s language.
3-2. Advantages of Web Service
Using Web services has the following advantages:
- Interoperability: Web Services allow applications to communicate, exchange data, and share services.
- 2. Availability: The functions of Web services can range from simple information search to complex algorithm calculation.
- 3. Reusability: Web services can be combined with each other to provide more complex services. Due to its interoperability characteristics, Web Service components can be easily reused in other services, improving the reuse rate of services.
- 4. Easy deployment: Web Services can be deployed in containers based on Internet standards, such as Apache, Axis2, etc., to provide HTTP or WSDL (Web Services Definition Language) driven services.
- 5. Low cost: Web services are packaged as Web Service components for deployment, thus reducing the cost of use.
3-3. Type of the Web Service
At present, there are two main schools of Web services:
- 1. SOAP based Web Service: SOAP(Simple Object Access Protocol) is an XML-based protocol for accessing Web services. Its interfaces are described in a machine-readable format called a WSDL(Web Services Definition Language) document. Web services are described using standard XML documents, in which detailed information about the interface, such as message format, transport protocol, and location of the interaction, is recorded.
- Rest-based Web Services: Representational State Transfer (REST) is a software architecture that uses JSON to describe data formats. Most importantly, the HTTP transport protocol is optional for REST.
3-4. Differences and connections between REST and SOAP
Here is a table to compare the similarities and differences between REST and SOAP:
conclusion
As mentioned above, we learned what an API is, what a REST API is, what a RESTful API is, and the related concepts of Web Services. API stands for Application Programming interface, a broad definition or protocol that serves as the interface between software programs to communicate with each other. REST apis are a subset of apis. All REST apis are apis; The RESTful API is an informal implementation of the REST API architectural style. Both apis and Web Services are means of communication between Service providers and Service consumers. Finally, in order to quickly identify the differences between API and Web Service, the differences between the two methods are summarized into the following comparison table:
Identify the differences and relationships between API & REST API & RESTful API & Web Service