In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how SpringMVC supports Restful style. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Preface
RESTFUL is a web application design style and development style, based on HTTP, you can use XML format definition or JSON format definition.
A brief introduction to RESTful
REST:Representational State Transfer, state transfer of resources in the presentation layer.
1.1. Resources
Resources are a way of looking at servers. That is, think of the server as consisting of many discrete resources.
Each resource is a namable abstract concept on the server.
Because resource is an abstract concept, it can not only represent a file in the server file system, a table in the database, etc., but also abstract as much as the resource is designed. as long as imagination permits and client application developers can understand.
Similar to object-oriented design, resources are organized with nouns as the core, with nouns as the first concern.
A resource can be identified by one or more URI. URI is both the name of the resource and the address of the resource on the Web. Client applications that are interested in a resource can interact with it through its URI.
1.2. Description of resources
The description of a resource is a description of the state of the resource at a particular time. It can be transferred (exchanged) between the client and the server.
Resources can be expressed in a variety of formats, such as HTML/XML/JSON/ plain text / pictures / video / audio, and so on.
The presentation format of resources can be determined through the negotiation mechanism. Request-response directions are usually expressed in different formats.
1.3, state transition
State transition refers to the transfer (transfer) between the client and the server that represents the state of the resource. Through the transfer and operation of the expression of resources, to indirectly achieve the purpose of operating resources.
RESTful specification
1. Standardize URL and write URL in RESTful format
Non-REST URL: http://..../queryItems.action?id=001
REST's URL style: http://..../items/001
Features: URL is concise, parameters are passed to the server through URL, and the request URL is only the location of the resource, so verbs should not appear.
2. The method specification of http
The URL used is the same regardless of whether it is deleted, added or updated. If you delete it, you need to set the HTTP method to delete. The same goes for others.
Background controller method: judge the http method, delete if it is delete, and add if it is post
3. ContentType specification for http
Execute contentType on request. If you want to json data, set it to type in json format.
However, the second point is often not achieved in practical use.
Second, the realization of RESTful
Specifically, there are four verbs in the HTTP protocol that denote the mode of operation: GET, POST, PUT, and DELETE.
They correspond to four basic operations: GET to obtain resources, POST to create new resources, PUT to update resources, and DELETE to delete resources.
REST style advocates the use of a unified style design for URL addresses. Words are separated by slashes from front to back. Instead of using question mark key-value pairs to carry request parameters, the data to be sent to the server is used as part of the URL address to ensure the consistency of the overall style.
Operation traditional REST style query operation getUserById?id=1user/1- > get request mode save operation saveUseruser- > post request mode delete operation deleteUser?id=1user/1- > delete request mode update operation updateUseruser- > put request mode 3, HiddenHttpMethodFilter
Q: since browsers only support sending get and post requests, how do you send put and delete requests?
A:SpringMVC provides HiddenHttpMethodFilter to help us convert POST requests into DELETE or PUT requests.
3.1.The HiddenHttpMethodFilter filter handles put and delete requests
Request:
The request method of the current request must be post.
The actual request method currently encapsulated by the post request is transmitted through the request parameter _ method.
The HiddenHttpMethodFilter filter converts the request mode of the current request to the value of the request parameter _ method, so the value of the request parameter _ method is the final request method.
3.2. register HiddenHttpMethodFilter HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter / * 3.3in web.xml, and the order of filter configuration
When two filters are provided in SpringMVC: CharacterEncodingFilter and HiddenHttpMethodFilter, when registering in web.xml, you must first register CharacterEncodingFilter and then register HiddenHttpMethodFilter.
Reason:
Setting the request.setCharacterEncoding (encoding) method of the character set through the request.setCharacterEncoding (encoding) method in CharacterEncodingFilter requires that there is no previous operation to get the request parameters.
And HiddenHttpMethodFilter has exactly one operation to get the request: String paramValue = request.getParameter (this.methodParam)
This is the end of SpringMVC's support for Restful style. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.