What is restful? What are the rest methods? What’s the difference?

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:

[what is restful? What are the rest methods? What are the differences?]

[small java class of Xiuzhen academy] what is restful? What are the rest methods? What are the differences?

Hello, I'm Zhao Linai, a student of the fourth phase of Xi'an Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge points of Java task 2 on the official website of the Academy. What is restful? What are the rest methods? What's the difference?

(1) Background:

Rest (representational state transfer)

It comes from an academic paper - "architectural styles and the design of network - based software architectures" published by Dr. Roy Thomas Fielding, the father of rest, during his study at the University of California, Irvine in 2000.

This paper puts forward six characteristics of rest: client server, stateless, cacheable, unified interface, layered system and on-demand coding.

Rest has the advantages of cross platform and cross language.

Rest is an architectural style.

In the rest architecture style, an object is abstracted as a resource The naming of resources is defined by clear nouns.

Declarative state is a snapshot of resource data in an instantaneous state,

A transient state of a resource is defined as a representation,

This descriptive state includes information such as the content and presentation format of resource data (such as XML and JSON),

A resource can correspond to multiple expressions.

Rest resources are addressable through the generic verb methods (such as get, put, delete, post) defined by the HTTP protocol (RFC 2616),

The URI protocol (RFC 3305) is used to uniquely identify the interface published by a resource.

The process of requesting a resource can be understood as accessing a specified and descriptive URI,

The representation of resources is transferred from the server to the client via HTTP, or in the opposite direction

(2) Knowledge analysis:

Rest request method

Each HTTP request method can be considered from two aspects: security and idempotence, which is of great significance for correctly understanding HTTP request methods and designing unified interfaces

Decisive significance. In other words, to define a rigorous rest unified interface, you need to really understand the security and idempotency of HTTP methods.

Security represents a secure rest interface. It refers to the access of external systems to this interface, which will not change the state of server-side resources.

Idempotence refers to multiple accesses to the same rest interface by an external system, resulting in the same resource state.

Get method

Rest uses the HTTP get method to obtain the resources provided by the service. If the get method is read-only, is it idempotent and secure?

The get method is idempotent because reading the same resource always gets the same data.

The get method is also safe because reading a resource does not change its state.

It is worth noting that although the characteristics of get methods are idempotent and secure, this does not mean that any method defined to process get requests is idempotent and secure

of Poorly designed APIs may violate the characteristics of get and define a method that should not be get.

Put method

The put method is an HTTP request for a write operation. Rest uses the put method of HTTP to update or add resources.

Update resources

The put method is idempotent, that is, the same data is inserted or updated multiple times, and the changes to the resource state on the server side are the same.

Put methods are not secure, and HTTP methods with write actions are not secure.

We know that when we use the same data to request the server to update a resource, the results should always be the same. Therefore, for the update operation,

There is no doubt about using put.

Add resource

The result of the creation operation is usually different each time, because the business layer logic on the server side usually requires

The primary key field of data is either from the business platform or from the database. Therefore, the same data

Each time it is submitted to the server, a new primary key value will be added to the data, that is, a new resource with different primary key values will be created

(if there is no business or foreign key conflict). Therefore, the creation operation should usually be designed as an API for the post method. Only one scenario should be used

Put method to design the API, that is, when the client initiates a creation request, it can always provide a unique primary key value in the same data, and the server will not modify it. Such a creation request ensures idempotency, and the post method should not be used again.

Delete method

The delete method is idempotent, that is, the same data is deleted multiple times (usually the parameter passed in the request is the primary key value of the data),

The changes on the server side are the same.

Post method

The post method is an HTTP request for a write operation.

Neither idempotent nor secure

The rest interface defined as post is used to write data. The characteristic of the post method is that it is neither idempotent nor secure.

Because the request will change the state of server-side resources, it is not secure; Each request does not change the server-side resource state the same,

So it's not idempotent.

(3) Frequently asked questions:

When to create and update a resource, when to use the HTTP put method and when to use the post method

(4) Solution:

Use put when updating resources

Post is used in most cases when creating resources

When the client initiates a creation request, it can always provide a unique primary key value in the same data, and the server will not modify it. Put is used for requests that can ensure idempotency

(5) Coding practice:

(6) Expand thinking:

Comparison between rest style and MVC style

The appearance of MVC style decouples the model, view and control. Its highlight is the consistency from front to back. Its structure is simple, logic is clear, and it is easy to expand and enhance.

MVC style focuses on solving the logical layering of the server, and the client is the extension of the logical layering.

MVC's tag library, although its form has been integrated with HTML pages,

However, it is essentially a class instance of the decoration pattern written in Java, corresponding to the model class or controller class written in Java on the server side. Therefore, it is difficult for MVC to realize cross language decoupling.

The rest style focuses on the unified interface, so the specific implementation can cross platform and cross language.

MVC and rest are not mutually exclusive. For example, spring's MVC module already supports rest development.

(7) References:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

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
分享
二维码
< <上一篇
下一篇>>