In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve REST and RESTful in spring. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Introduce the concept of REST:
REST: full name: Representational State Transfer, meaning: state transfer of resources in the presentation layer
The detailed explanation can be divided into three parts:
Resources
Resources are a way to look at the server, that is, to 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.
Representation 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.
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.
REST makes good use of some of the features inherent in HTTP, such as HTTP verbs, HTTP status codes, HTTP headers, etc.
The main principles of the REST architecture:
There is a resource identifier for all resources on the network.
Operations on resources do not change identifiers
There are multiple manifestations of the same resource (xml, json)
All operations are Stateless
The architectural approach that conforms to the above REST principles is called RESTful.
RESTful concept:
RESTful is a common REST application, which follows the REST-style web service. REST-style (rest has an additional ful compared to restful, which is an adjective in English, and restful is translated into Chinese as "rest-style") web service is a ROA (resource-oriented architecture)
The connection and difference between REST and RESTful: restful is derived from rest.
RESTful implementation
Specifically, in the http protocol, there are four verbs indicating the mode of operation: get,post,put,delete, which correspond to four basic operations: get to obtain resources, post to create resources, put to update resources, and delete to delete resources.
REST style advocates the use of a unified style design for URL addresses, with slashes separating words from front to back, instead of question mark key-value pairs to carry request parameters, but the data to be sent to the server as part of the URL address to ensure the consistency of the overall style.
GET, query all user information / * use RESTFul to simulate the addition, deletion, modification and query of user resources * / user GET query all user information * / @ RequestMapping (value = "/ user", method = RequestMethod.GET) public String getAllUser () {System.out.println ("query all user information"); return "success";} query all user information
GET, query user information according to user ID / * use RESTFul to simulate the addition, deletion, modification and query of user resources * / user GET query user information according to user id * / @ RequestMapping (value = "/ user/ {id}", method = RequestMethod.GET) public String getUserById () {System.out.println ("query user information according to id"); return "success" } query user information according to id
POST, add user information / * use RESTFul to simulate the addition, deletion, modification and query of user resources * / user POST add user information * / @ RequestMapping (value = "/ user", method = RequestMethod.POST) public String insertUser (String username, String password) {System.out.println ("add user information:" + username+ "," + password "); return" success ";} user name:
Password:
PUT, modify user information / * use RESTFul to simulate the addition, deletion, modification and query of user resources * / user PUT modify user information * / @ RequestMapping (value = "/ user", method = RequestMethod.PUT) public String updateUser (String username, String password) {System.out.println ("modify user information:" + username+ "," + password "); return" success ";} user name:
Password:
Note:
Since browsers only support sending get and post requests, how do you send put and delete requests?
SpringMVC provides HiddenHttpMethodFilter to help us convert POST requests into DELETE or PUT requests
The conditions for HiddenHttpMethodFilter to handle put and delete requests: a > the request method of the current request must be post, and b > the request parameter _ method must be transmitted for the current request. If the above conditions are met, the HiddenHttpMethodFilter filter will convert 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.
Configure HiddenHttpMethodFilter, in web.xml
HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter / *
Note:
So far, two filters have been provided in SpringMVC: CharacterEncodingFilter (encoding filter) and HiddenHttpMethodFilter (request mode filter). When registering in web.xml, you must first register CharacterEncodingFilter and then register HiddenHttpMethodFilter. Reason:
The request.setCharacterEncoding (encoding) method of setting the character set through the request.setCharacterEncoding (encoding) method in CharacterEncodingFilter requires that there is no previous operation to get the request parameters, while HiddenHttpMethodFilter has exactly one operation that gets the request method:
String paramValue = request.getParameter (this.methodParam); that's all about the article "how to implement REST and RESTful in spring". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.