[Spring Boot] Building RESTful Services - Introduction to RESTful

Introduction to RESTful

This section will start from the basic concepts to introduce what is RESTful, the characteristics of RESTful, the resources in RESTful, HTTP Method, HTTP Status, and also introduce the differences between RESTful and SOAP.

1. What is RESTful

RESTful is currently a popular Internet software service architecture design style. The term REST (Representational State Transfer) was proposed by Roy Thomas Fielding in his doctoral dissertation in 2000. It defines the architectural principles of Internet software services. If an architecture conforms to the REST principles, it is called RESTful architecture.

REST is not a standard. It is more like a set of architectural concepts and design principles for the interaction between clients and servers. Web APIs based on this architectural concept and design principles are more concise and layered.

1.1 Features of RESTful

1) Each URI represents a resource.

2) The client uses GET, POST, PUT, and DELETE to operate on the server resources: GET is used to obtain resources, POST is used to create new resources (it can also be used to update resources), and PUT is used to update resources, DELETE is used to delete resources.

3) Realize the server-side request operation by manipulating the representation of resources.

4) The representation of the resource is JSON or HTML.

5) The interaction between the client and the server is stateless between requests, and each request from the client to the server contains the necessary information.

A Web API conforming to the RESTful specification needs to have the following two key features:

  • Safety: Safe methods are expected not to have any side effects. When we use the GET operation to obtain a resource, it will not cause the resource itself to change, nor will it cause the server state to change.
  • Idempotency: The idempotent method ensures that repeated requests have the same effect as one request (it does not mean that the response returned to the client is always the same, but that the state of the resources on the server is no longer the same after the first request) Change). In mathematics, idempotency refers to the result of N transformations and one transformation being the same.

1.2 Background of REST

With the development of the Internet, the data interaction between the front-end page and the back-end is becoming more and more frequent, and the data structure is becoming more and more complex. The emergence of REST greatly simplifies the interaction logic of the front-end and back-end data. If we regard the front-end page as a client for display, then the API is an interface for providing data and manipulating data for the client. This design can achieve extremely high scalability.

Assume that everyone originally shopped through the online mall on the PC. When it needs to be extended to mobile terminals such as mobile phones, it only needs to develop two clients for iOS and Android, and access the public Web API of the system through the client. The functions provided by the page, and the back-end code basically does not need to be changed, as shown in the figure.

insert image description here

The RESTful-style Web API supports us to use a unified interface specification to connect to clients such as iOS, Android, HTML5, and PC. It is precisely because REST has many advantages that once REST was proposed, it quickly replaced the complex and cumbersome SOAP and became the standard of Web API.

2.HTTP Method

What is HTTP Method (HTTP method)?

HTTP provides operation types such as POST, GET, PUT, and DELETE to perform Create, Read, Update, and Delete operations on a web resource. In addition to using the URI to mark the target resource, an HTTP request also needs to specify the operation type for the resource through the HTTP Method. The following table introduces some common HTTP methods and their use in RESTful style.

insert image description here

Summarizes the suggested return values ​​for the major HTTP methods used in conjunction with resource URIs. Common HTTP Methods (HTTP methods) include POST, GET, PUT, PATCH, and DELETE, which correspond to Create, Read, Update, and Delete (or CURD) operations, respectively. Of course, there are many other methods, such as OPTIONS and HEAD, etc., but they are used less frequently.

3. HTTP status code

The HTTP status code is the status code and prompt information returned by the service to the user. For each request from the client, the service must respond. The response includes two parts: the HTTP status code and the data.

HTTP defines 40 standard status codes that can be used to communicate the result of a client request. Status codes are divided into the following 5 categories:

  • 1xx: Information, communication transfers protocol-level information.
  • 2xx: Success, indicating that the client's request has been successfully accepted.
  • 3xx: Redirection, indicating that the client must perform some additional action to complete its request.
  • 4xx: Client error, this type of error status code points to the client.
  • 5xx: Server Error, the server is responsible for these error status codes.

RESTful APIs use HTTP status codes to indicate the status of request execution results, and the codes and corresponding HTTP methods applicable to REST API design are shown in the table.

insert image description here
The status code and HTTP Method provided by the HTTP protocol. The status code and prompt information returned to the client by the RESTful API can determine the request and execution status of the Web API.

In addition to the above basic HTTP request status codes, the Web API server also needs to define business-related status, such as 1000 order submitted successfully, 1002 order modified successfully, etc. Each status code has a standard interpretation. The client only needs to check the status code dictionary to know the execution result of the corresponding business, so the server should return the status code as accurate as possible.

4. The difference between REST and SOAP

With the development of the Internet, RESTful is becoming more and more popular, so what is the difference between RESTful and SOAP? When we design Web services, should we choose the most popular RESTful or the old WebService?

SOAP (Simple Object Access Protocol, Simple Object Access Protocol) is a standardized communication specification, mainly used in Web services. It has strict specifications and standards, including security, transaction and other aspects. At the same time, SOAP emphasizes the separation of operation methods and operation objects, and uses WSDL file specification and XSD file to define them respectively.

RESTful simplifies the design of WebService, it no longer needs WSDL, but transmits data (including XML or JSON) through the simplest HTTP protocol. It not only simplifies the design, but also reduces the amount of network transmission (because only the XML or JSON representing the data is transmitted, without additional XML packaging).

REST enforces that all operations must be stateless, without context constraints, and does not need to consider the issues of context and session retention, which greatly improves the scalability of the system. Compared with SOAP, RESTful is simpler and clearer. It does not have a clear architectural standard. It is more like a design style. Its core is resource-oriented; while WebService is based on the SOAP protocol, and its main core is activity-oriented.

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/132169330