Correctly identify the similarities and differences among API, REST API, RESTful API and Web Service

What do you think of when you see the API? Is it an interface, a third-party call, or an API document? At first glance, you may feel that this is too familiar. Isn’t this the daily series of system development? But if you think about it carefully, you will find that the concept of API is so vague in your mind. If you search the API through a search engine, you will see information like this: API–Application Programming Interface (application programming interface), which is too abstract. Next, I will talk about the connection and differences between API, REST API, RESTful API and Web Service in a popular way based on some experience I have summarized in development.

1. API and REST API

What is API? Here is the definition given by Wikipedia: Application Programming Interface (English: Application Programming Interface, abbreviation: API; also known as application programming interface) is a convention for the connection of different components of a software system. This definition of API is too broad and abstract. Generally speaking, API is a way (protocol) for an application to “communicate” with another application. In web application development, API is a main way for us to retrieve data through the network. The API document will inform you of the URL list, query parameters, request method and response status of the retrieved data, the purpose of which is to reduce the difficulty of web application development , share data (text, audio, video, pictures, etc.) between two applications, while shielding its internal complex implementation details. ?

REST is the abbreviation of Representational State Transfer, literally translated as: expressing the transfer of state. REST API is a set of architectural rules, standards or guidelines on how to build web application APIs, or REST API is an architectural style that follows API principles. REST is specially designed for web applications, and its purpose is to reduce the complexity of development and improve the scalability of the system. The following are some basic conditions and principles that need to be met or followed when designing a REST-style system architecture:

  • 1. In the REST architecture, all things on the Web (text, audio, video, pictures, links) can be unified and abstracted as resources (resources)
  • 2. In the REST architecture, each resource has a corresponding unique resource identifier (resource identifier). When the state of the resource changes, the resource identifier will not change
  • 3. In the REST architecture, all operations are stateless. The REST architecture follows the CRUD principle, and all resources can complete corresponding operations through the four behaviors of GET, POST, PUT, and DELETE.
  • 4. Cacheable (optional), cache is required in the REST architecture to effectively handle large batches of requests
  • 5. The interface is consistent

Now, knowing the basic concepts of API and REST API, what are the similarities and differences between the two? If the connection and difference between API and REST API are explained according to the concept of mathematical collection, 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 is a REST API. 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, understand what is REST API, and then talk about the similarities and differences between REST API and RESTful API. Many beginners easily equate the two and think that RESTful API is REST API. This may be understood literally. When you deeply understand the essence of the two, you will find that it is not the case. REST API is a specification or guiding principle of Web API design, and RESTful API is a specific implementation of this architectural design principle or specification. In other words, RESTful API is an informal implementation of REST API, because there are many ways to implement REST API, and RESTful API is just one of them, and it does not fully satisfy all the design principles of REST API. Every developer implements REST architecture Sometimes the emphasis will be different.

Many beginners tend to confuse the concepts of REST API and RESTful API. I think they may just look at the literal meaning, but not pay attention to their own meaning (just like knowing Chinese characters, read while reading, read in the middle without reading, and take meaning out of context up). This is like many people will equate transgender people with women. Transgender people may have the same facial features as women, but transgender people can’t bear children. They just meet most of the conditions (realization) that define a woman, but they are not in essence. woman.

Next, a simple example is used to deepen the understanding of REST API and RESTful API. The following will give a RESTful API design case for performing CURD operations:

Note: resource refers to the name of a certain resource, which can be student (student), teacher (teacher), book (book), etc., usually expressed by nouns; {id} refers to the unique identifier of a certain resource ( resource identifier)

The following is a specific small example, taking student management as an example to design the API for student management. Student resources include ID, name and course information. The student resource information is as follows:

Now, we need to save the student data to the database, and then perform operations to query, modify, and delete the student data. The API called by the user of the student management API is as follows:

  • 1. Create student resources: [POST] http://www.example.com/student
  • 2. Get all student resources: [GET] http://www.example.com/students
  • 3. Get student resources with ID=1001: [GET] http://www.example.com/student/1001
  • 4. Modify the student resource with ID=1001: [PUT] http://www.example.com/student/1001
  • 5. Delete the student resource with ID=1001: [DELETE] http://www.example.com/student/1001

As mentioned above, APIs share data resources while shielding internal implementation. API users (clients) focus on resources (reading data), and do not need to understand the internal structure of APIs; API providers (servers) Only focus on its own internal implementation, not the status of API users (clients). In order to deepen the understanding of this concept, an example of the internal implementation of the student management API is given below:

Note: The sample code is implemented based on Spring MVC.

In addition to the above content, you can also filter the query data by providing key-value pairs. For example, when obtaining all student data, you only want to obtain the student data whose gender is female, you can do this in this way:

[GET] http://www.example.com/students?gender=female

Tip: If the API has the function of data filtering, the API implementation code on the corresponding server side also needs to be adjusted.

In the previous content, we mentioned that RESTful API is an informal implementation or specification of REST API. Why do you say that? Because in the design of RESTful API, we can complete the CURD operation through GET, create resources through DELETE behavior, and modify resources through POST behavior. Its implementation method is not strict or does not strictly follow the REST API. The proposed constraints are carried out. So RESTful API is an informal implementation of REST API.

3. REST and Web Services

3-1. What is Web Service?

As stated by the World Wide Web Consortium (W3C), Web Services provide a standard method for interoperability between different software applications running on various platforms and/or frameworks. The characteristic of Web Service is that it has good interoperability and expansibility, and it can describe the program processing process because of the use of XML. 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.

The communication between two Web Services is mainly through the HTTP network protocol, such as the well-known SOA (Service Oriented Architecture), which mainly relies on XML-RPC and SOAP (Simple Object Access Protocol, that is, Simple Object Access Protocol).

Tip: Do not confuse SOA (Service Oriented Architecture) with SOAP (Simple Object Access Protocol). The former is a form of architectural design, while the latter is a data exchange protocol.

A simple example: Suppose a Web Service A provides the function of allowing other applications to obtain user information through the URL: [GET] http://www.abc.com/{id}. id is the unique identifier of the user, requesting this URL will get user information. Now assume that users of browsers, mobile phones, and desktop applications all need to obtain the user information provided by service A. These three only need to request the URL address provided by service A and enter the user id information. As for the implementation methods (programming languages) of the three different clients, it has nothing to do with service A, as long as the XML document returned by service A can be parsed. In this way, data can be exchanged between applications without depending on the specific language and environment. This is like people with different languages in different countries. As long as they can understand the grammatical structure of each other’s language, two people can communicate.

3-2. Advantages of Web Service

Using Web Service has the following advantages:

  • 1. Interoperability: Web Service allows applications to communicate, exchange data and share services.
  • 2. Usability: The functions of Web Service 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, Web Service components can be easily reused in other services, improving the reuse rate of services.
  • 4. Easy to deploy: Web Service can be deployed in containers based on Internet standards, such as Apache, Axis2, etc., to provide services driven by HTTP or WSDL (Web Service Definition Language).
  • 5. Low cost: Web Service is deployed by packaging into Web service components, thus reducing the cost of use.

3-3. Types of Web Services

At present, there are two main schools of Web Service:

  • 1. SOAP-based Web Service: SOAP (Simple Object Access Protocol) is an XML-based protocol for accessing Web Services. Its interface is described in a machine-processable format called a WSDL (Web Services Definition Language) document. The Web Service is described by using a standard XML document. In the XML file, the information of the interface will be recorded in detail, such as the format of the message, the transmission protocol, and the location of the interaction.
  • 2. REST-based Web Service: REST (Representational State Transfer) is a software architecture that uses JSON to describe the data format. The most important thing is that the HTTP transfer protocol is not necessary for REST.

As mentioned above, we understand what is API, what is REST API, what is RESTful API and related concepts of Web Service. API stands for application programming interface, which is a relatively broad definition or a protocol that exists as an interface for mutual communication between software programs. REST API is a subset of API, and all REST APIs are APIs; RESTful API is an informal implementation of the REST API architectural style. Both API and Web Service are means of communication between service providers and service consumers. Finally, in order to quickly identify the difference between API and Web Service.