JavaWeb: Talk about Restful interface design style

I. Introduction

This article introduces the background interface design style-Restful style, which is divided into four parts. The second part explains a Restful Chinese meaning and gives that to truly understand the Restful style, it is necessary to take it apart from its Chinese name to understand; The third part splits the Restful Chinese name "representation state transition of resources" into three words and understands them one by one; the fourth part introduces the specific requirements of the Restful design style; the fifth part lists two of them using the Restful style mistake.

2. What does Restful mean?

REST, English full name Representational State Transfer, translated as "presentational state transfer".

If an architecture conforms to REST principles, it is called a RESTful architecture .

Note: In practical applications, Restful is just an interface design style. If the URL and parameters of an interface conform to the REST principle, it is called a RESTful design style. If the URL and parameters of an interface do not conform to the REST principle, it is said to be inconsistent. RESTful design style, but it cannot be said that it is wrong. For a background interface, as long as the function can be used normally, there is no fixed rule for the design of URLs and parameters, but it does not conform to the Restful style.

To understand the RESTful architecture, the best way is to understand what the expression Representational State Transfer means and what each word represents. If you understand this name, it is not difficult to understand what kind of design REST is.

Note: Restful is just a style. It has nothing to do with the language. It can be defined in XML format or JSON format.

Three, from the name to thoroughly understand the Restful style

3.1 Resources

In the name of REST "state transition of presentation layer", the subject is omitted. The "presentation layer" actually refers to the "representation layer" of "Resources".

The so-called "resource" is an entity on the network, or specific information on the network. It can be a text, a picture, a song, a service, in short, it is a concrete reality. You can point to it with a URI (Uniform Resource Locator), and each resource corresponds to a specific URI. To obtain this resource, you can access its URI, so the URI becomes the address or unique identifier of each resource.

The so-called "online" is to interact with a series of "resources" on the Internet and call its URI.

3.2 Representation

"Resource" is an information entity, which can have multiple external manifestations. We refer to the form of "resources" as its "representation".

For example, text can be expressed in txt format, HTML format, XML format, JSON format, or even binary format; pictures can be expressed in JPG format or PNG format.

URI only represents the entity of the resource, not its form. Strictly speaking, the ".html" suffix name at the end of some URLs is unnecessary, because this suffix name represents the format and belongs to the "representation layer" category, and the URI should only represent the location of "resources". Its specific form of expression should be specified in the header of the HTTP request using the fields Accept and Content-Type, which are the descriptions of the "presentation layer".

3.3 State Transfer

Visiting a website represents an interactive process between the client and the server. In this process, it is bound to involve changes in data and status.

The Internet communication protocol HTTP protocol is a stateless protocol. This means that all state is saved on the server side. Therefore, if the client wants to operate the server, it must use some means to allow the "State Transfer" to occur on the server side. And this transformation is based on the presentation layer, so it is "state transformation of the presentation layer".

The method used by the client can only be the HTTP protocol. Specifically, there are four verbs in the HTTP protocol that indicate the operation mode: GET, POST, PUT, and DELETE. They correspond to four basic operations: GET is used to obtain resources, POST is used to create new resources (which can also be used to update resources), PUT is used to update resources, and DELETE is used to delete resources.

4. What exactly is the Restful style?

Based on the above explanation, we summarize what is the RESTful design style:

(1) Each URI represents a resource;

(2) Between the client and the server, some kind of presentation layer for transferring such resources;

(3) The client operates the server-side resources through four HTTP verbs to achieve "presentation state transition".

Note: Restful is just a style. It has nothing to do with the language. It can be defined in XML format or JSON format.

5. Several mistakes made when using Restful style

RESTful architecture has some typical errors.

5.1 URI contains verbs

Because "resource" represents an entity, it should be a noun, URI should not have verbs, verbs should be placed in the HTTP protocol.

For example, a URI is / posts / show / 1, where show is a verb, the URI is wrongly designed, the correct wording should be / posts / 1, and then use the GET method to show.

If some actions cannot be expressed by HTTP verbs, you should make the actions a resource. For example, online remittance, 500 yuan is transferred from account 1 to account 2, the wrong URI is:

POST /accounts/1/transfer/500/to/2

Note: It is only said that the uri written for this interface does not conform to the Restful style, but only does not conform to the Restful style. It is not that the interface is wrong.

The correct way of writing is to change the verb transfer to a noun transaction. The resource cannot be a verb, but it can be a service:

POST /transaction HTTP/1.1
Host: 127.0.0.1
  
from=1&to=2&amount=500.00

5.2 Add version number to URI

Another design misunderstanding is to add the version number in the URI:

http://www.example.com/app/1.0/foo

http://www.example.com/app/1.1/foo

http://www.example.com/app/2.0/foo

Note: It is only said that the uri written for this interface does not conform to the Restful style, but only does not conform to the Restful style. It is not that the interface is wrong.

Because different versions can be understood as different manifestations of the same resource, the same URI should be used. The version number can be distinguished in the Accept field of the HTTP request header information (see Versioning REST Services):

Accept: vnd.example-com.foo+json; version=1.0

Accept: vnd.example-com.foo+json; version=1.1

Accept: vnd.example-com.foo+json; version=2.0

6. Summary

This article introduces the background interface design style-Restful style, which is divided into four parts. The second part explains a Restful Chinese meaning and gives that to truly understand the Restful style, it is necessary to take it apart from its Chinese name to understand; The third part splits the Restful Chinese name "representation state transition of resources" into three words and understands them one by one; the fourth part introduces the specific requirements of the Restful design style; the fifth part lists two of them using the Restful style mistake.

Code every day, make progress every day!

Published 207 original articles · praised 80 · 120,000 views

Guess you like

Origin blog.csdn.net/qq_36963950/article/details/105617910