RESTful introduction and tutorial

1. REST origin

REST (Representational State Transfer) representational state transition (representational state transition) was proposed in 2000. Based on HTTP, URI, XML, JSON and other standards and protocols, it supports lightweight, cross-platform, and cross-language architecture design. Is a new architectural style (an idea) of Web services.

1. What is lightweight:

The code is not intruded (positive example: SpringMVC does not use interfaces and inheritance, only annotations are used. Counter example: every Action in Struts must inherit the core controller), and the lightweight has nothing to do with the size of the package. The lower the coupling, the lighter the weight.

2. The main principles of REST architecture

  • There is a resource identifier for all resources on the network.

  • Operations on the resource do not change the identifier.

  • The same resource has multiple representations (xml, json)

  • All operations are stateless

An architectural approach that conforms to the above REST principles is called RESTful

1. The difference between URI and URL:

URI:http://example.com/users/

URL:http://example.com/users/{user} (one for each user)

2. What is statelessness:

So that the client and the server do not need to save each other's detailed information, the server only needs to process the current request, and does not need to know the history of the request. It is easier to release resources and let the server use Pool (connection pool) technology to improve stability and performance.

write picture description here

3. Introduction to RESTful

RESTful is a common REST application, and it is a web service that follows the REST style. The REST style web service is a kind of ROA (resource-oriented architecture).

1. RESTful resource operations

http method resource operation idempotent Safety
GET SELECT Yes Yes
POST INSERT no no
PUT UPDATE Yes no
DELETE DELETE Yes no

Idempotency: Multiple accesses to the same REST interface result in the same resource state.

Security: Accessing the REST interface will not change the state of server-side resources.

2. Interface example:

2.1. Traditional URL request format:

http://127.0.0.1/user/query/1 GET Query user data based on user id

http://127.0.0.1/user/save POST add user

http://127.0.0.1/user/update POST Modify user information

http://127.0.0.1/user/delete GET/POST delete user information

2.2.RESTful request format:

http://127.0.0.1/user/1 GET Query user data based on user id

http://127.0.0.1/user POST Add new user

http://127.0.0.1/user PUT Modify user information

http://127.0.0.1/user DELETE delete user information

3. Responsive Design

Principle: Data is ready to use as received, without unboxing.

In a request, the content body is only used to transmit data. Header stores metadata describing the request or request, such as X-Result-Fields.
write picture description here

4. http response status code

According to the http response code, determine the request status, and then make a reminder.
write picture description here

4. Use of RESTful


The following will use the actual code to demonstrate the use of RESTful to add, delete, modify, and query:

1. Query get:

write picture description here

Query test:

write picture description here
write picture description here

2. New post:

write picture description here

Added test:

AJAX call:
write picture description here
write picture description here

3. Update put:

write picture description here

Service writing:

write picture description here

Update test:

write picture description here

By default, PUT requests cannot submit form data. In Spring MVC projects, you need to add filters to web.xml to solve:

  <!-- 解决PUT请求无法提交表单数据的问题 -->
   <filter>
      <filter-name>HttpMethodFilter</filter-name>
      <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
   </filter>
   <filter-mapping>
      <filter-name>HttpMethodFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

4. Delete delete:

write picture description here

delete test:

write picture description here

5. Conclusion

So far, the introduction and use of RESTful has been completed. RESTful has become the mainstream paradigm in the interface definition of various Internet companies. RESTful reduces the unpacking operation of traditional requests, and has a clear structure. It is favored by front-end and back-end developers in the interface definition. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324107453&siteId=291194637