Why use the rest style? If you don’t use rest, how should the interface be defined? How do you write the interface before using the rest style?

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

Why use the rest style? If you don't use rest, how should the interface be defined? How do you write the interface before using the rest style?

Hello, I'm Li Yuhang, a student of the 12th phase of Zhengzhou branch of it Academy. I'm an honest, pure and kind-hearted back-end programmer. Today, I'd like to share with you Java (career) task 2 on the official website of the Academy,

Knowledge points in deep thinking - why use the rest style? If not, how should the interface be defined? How did everyone write the interface before using the rest style?

1. Background introduction

Rest (representative state transfer) refers to an architectural design style, and applications or designs that meet this design style are considered restful architectural style.

This is also the most popular software architecture style in the Internet. It has clear structure, conforms to standards and is easy to understand. More and more application services begin to use restful architecture style, especially those network services based on HTTP protocol.

The web service of rest, which is often mentioned, is an implementation of rest in the web field

2. Knowledge analysis

The so-called "resource" is an entity on the network, or a 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 get this resource, you can access its URI, so the URI becomes the address or unique identifier of each resource.

Presentation layer

"Resource" is an information entity, which can have a variety of external manifestations. We call the concrete form of "resource" its "Representation".

The URI only represents the entity of the resource, not its form.

Strictly speaking, the ". HTML" suffix at the end of some web addresses is unnecessary, because this suffix represents the format and belongs to the category of "presentation layer", and the URI should only represent the location of "resources".

Its specific manifestation should be specified in the header information of the HTTP request with accept and content type fields, which are the description of the "presentation layer".

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 state.

Internet Communication Protocol HTTP protocol is a stateless protocol. This means that all States are saved on the server side.

The means used by the client can only be HTTP protocol. Specifically, in the HTTP protocol, there are four verbs representing 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 (or update resources), put is used to update resources, and delete is used to delete resources.

Rest style summary

1. Rest describes an interactive form between client and server in the network; Rest itself is not practical, but how to design restful API (rest style network interface);

2. In the restful API provided by the server, only nouns are used to specify resources in the URL, and verbs are not used in principle. "Resource" is the core of rest architecture or the whole network processing, which represents a network entity and represents a resource; It corresponds to the only resource and is the address of the resource.

3. Use verbs in HTTP protocol to add, modify and delete resources.

4. A representation of a resource transferred between server and client

5. Use HTTP status code to pass server status information.

3. Frequently asked questions

1. @ pathvariable @ requestparam common points

A: for receiving requests from the request, both can receive parameters

2@pathvariable @Requestparam difference

A: the Chinese meaning of requestparam is: request parameters, as the name suggests, are used to obtain parameters

Pathvariable in Chinese means: path variable, as the name suggests, is to obtain part of the value of a URL address

4. Coding practice

5. Expand thinking

What is idempotent operation? Why is idempotency important?

Idempotence

, the power equality simply means that the state of a resource is the same for one request and multiple requests.

For example, get and head, no matter how many times you request, the resource is still there.

Please note that delete and put are also equal powers. It is assumed that if the same resource is deleted one or more times, the result is the same, that is, the resource is deleted and does not exist.

Why is put equal to power? When you put a new resource, the resource is created. When you put the URI again, the resource remains unchanged.

When you put an existing resource, the resource is updated. When you put it again, it is updated like this.

When updating put, relative updates (depending on the current state of the resource) cannot be made. For example, add 1 to a number every time, so the resource state will change.

It should be updated to a number every time. For example, if a number is changed to 4, no matter how many times put, the value is 4, which is equal power.

When we design restful WS, get, head, put and delete must be designed to equal power.

Because the network is unreliable, security and power equality are particularly important.

If a request is received and processed by the server, and the client does not receive the corresponding, the client will request again. If there is no power guarantee, unexpected problems will occur.

Post is unsafe and unequal. Take weblog as an example. If you post the same blog twice, two resources will be generated. The URIs may be / weblogs / myweblog / entries / 1 and / weblogs / myweblog / entries / 2, although their contents are the same.

6. References

https://blog.csdn.net/xinjianwuhen1991/article/details/50095021

http://www.ruanyifeng.com/blog/2011/09/restful.html

7. More discussion

1. Why use restful structure?

A: with the development of mobile Internet in recent years, various types of clients emerge in endlessly. Restful can provide services for web, IOS and Android through a set of unified interfaces.

With a restful structure, whether it is a computer or a mobile phone, or an app, data interaction is carried out according to the rest interface, and there is no need to care about the back-end implementation. In other words, the front-end and back-end really realize a complete separation design.

2. How do I write interfaces before using rest

A: the URL can be written as it should be, but there are no put, get, post or delete methods

3.1. What is the difference between redirection and forwarding?

A: redirect and forward. Redirection can take data, but forwarding cannot. Forwarding is server behavior and redirection is client behavior.

This is the end of sharing

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>