This is the 16th day of my participation in Gwen Challenge

1. What is RESTful

Restful is a software architecture style, a design style, not a standard, but a set of design principles and constraints. It is mainly used for client and server interaction class software. Software designed in this style can be simpler, more hierarchical, and easier to implement mechanisms such as caching. In simple terms, it provides a rule to make the API more formal and easy to understand.

  1. REST the interpretation of the

REST is an acronym for Representational State Transfer, which translates as “Representational State transformation.” The presentation layer is a form of rendering resources, like HTML; State transition refers to changing the state of the server (data and state changes, etc.); I feel like it’s a name for the back and forth interaction process.

  1. The most important architectural constraints of the REST architectural style are six:
  • Client-server

Communication can only be initiated unilaterally by the client in the form of request-response.

  • Stateless

The Session State of the communication should be completely maintained by the client.

  • Cache

The response content can be cached somewhere in the communication chain to improve network efficiency.

  • Uniform Interface

Components of the communication chain communicate with each other through a unified interface to improve the visibility of the interaction.

  • Layered System

The architecture is decomposed into several levels of layers by limiting the behavior of components (that is, each component can only “see” the immediate layer with which it interacts).

  • Code On Demand (optional)

Supports the extension of client functions by downloading and executing code such as Java applets, Flash, or JavaScript. 3. Under what circumstances can it be used in interface design that focuses on resource mobilization

2. Why RESTful style

  • More semantic, easy to understand
  • Security and idempotent (sending the same request once or more has the same effect on the server)
  • Make your API design resource-oriented

Because each URI represents a resource, no verbs should be used in API design, and get, update, and delete should not appear in urIs.

3. How to use it

  1. The verbs that RESTful style requires

Security: GET (SELECT) : Retrieve one or more resources from the server. Idempotent: POST (CREATE) : creates a new resource on the server. PUT (UPDATE) : updates the resource on the server. PATCH (UPDATE) : Updates resources on the server (the client provides the changed properties). DELETE (DELETE) : deletes resources from the server. Uncommon verb HEAD: to obtain metadata of a resource. OPTIONS: Get information about which properties of the resource can be changed by the client. — Ruan Yifeng’s web log

These verbs are provided by the HTTP protocol

  1. The front-end takes Axios as an example
// Use method instead of axios.
// Post requires jSON-formatted data to be followed by the URI
this.axios.delete(`/users/`+id
)
.then((response) = > {
    console.log(error);
})
.catch((error) = > {
    console.log(error);
});
Copy the code
  1. The backend uses springMVC as an example
@RestController
@RequestMapping("/users")
public class userController{
	@RequestMappering(value="/{id}",method=RequestMethod.DELETE)
	public void delete(@PathVariable("id")int id){}}Copy the code

That’s the idea

  1. Some common uses

[GET] /users # query user information list

【GET】 /users/1001 #

[POST] /users # create user info

【PUT】 /users # update user information

[PATCH] /users/ #

[DELETE] /users/1001

By changing parameters to achieve functional expansion, like [GET], no passed parameters, is the query all; Passed an ID, is query query a user; Passing a set of ids is a query for a set of users. Each GET corresponds to a method on the back end.

  1. Common mistakes

Uris should not contain verbs; use all nouns, plural if possible. If you return to the view, change the input value whose name is _method in the form to GET, POST, and HEAD. Do not use PUT or DELETE.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support

Shall not be reproduced without permission

Wechat search [programmer Xu Xiaobai], attention can be the first time to read the latest article. There are 50 high-frequency school recruitment interview questions prepared by me, as well as a variety of learning materials.