In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve the Restful style of Spring MVC, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Let's start with the argument: I think the Restful style is just a style, not a sophisticated technical architecture, but a programming specification. In the process of our application development, we can find that more than 80% of the operations are add, delete, query and modify operations. Restful defines the development specification of CRUD. Here is a comparison between restful style url and traditional style url.
Business operation
Traditional style URL
Traditional request method
Restful style URL
Restful request mode
Add
/ add
GET/POST
/ order
POST
Modify
/ update?id=1
GET/POST/order/1
PUT
Query
/ get?id=1GET/POST/order/1GET
Delete
/ delete?id=1GET/POST/order/1DELETE
Since we learned jsp and servlet, the request method used is generally post or get, and browsers currently only support post and get requests, so how to implement put and delete requests? Don't worry, spring mvc provides a filter to implement put and delete requests. Let's take a look at the filter code, briefly talk about the understanding of gaze, the level is limited, not necessarily accurate: browsers currently only support post and get requests, this filter can realize the conversion of post requests into put requests or delete requests, how is it implemented? Using a normal post request, plus a hidden field, the name of the hidden field is _ method, and when it finds that it is a post request, and there is a _ method attribute, it converts the request into a http request for the response. It is also important to note that the filter is defined in the location of the web.xml, because the filter needs to check the parameters of the post, so it needs to be defined after the file upload filter, typically: org.springframework.web.multipart.support.MultipartFilter.
Package org.springframework.web.filter;import java.io.IOException;import java.util.Locale;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;import org.springframework.util.Assert;import org.springframework.util.StringUtils;import org.springframework.web.util.WebUtils / * * {@ link javax.servlet.Filter} that converts posted method * parameters into HTTP methods, retrievable via * {@ link HttpServletRequest#getMethod ()} Since browsers currently only * support GET and POST, a common technique-used by the Prototype * library, for instance-is to use a normal POST with an additional * hidden form field ({@ code _ method}) to pass the "real" HTTP method along. * This filter reads that parameter and changes * the {@ link HttpServletRequestWrapper#getMethod ()} return value accordingly. * *
The name of the request parameter defaults to {@ code _ method}, but can be * adapted via the {@ link # setMethodParam (String) methodParam} property. * *
NOTE: This filter needs to run after multipart processing in case of * a multipart POST request, due to its inherent need for checking a POST * body parameter. * So typically, put a Spring * {@ link org.springframework.web.multipart.support.MultipartFilter} * before this HiddenHttpMethodFilter in your {@ code web.xml} filter chain. * * @ author Arjen Poutsma * @ author Juergen Hoeller * @ since 3.0 * / public class HiddenHttpMethodFilter extends OncePerRequestFilter {/ * * Default method parameter: {@ code _ method} * / public static final String DEFAULT_METHOD_PARAM = "_ method"; private String methodParam = DEFAULT_METHOD_PARAM; / * Set the parameter name to look for HTTP methods. * @ see # DEFAULT_METHOD_PARAM * / public void setMethodParam (String methodParam) {Assert.hasText (methodParam, "'methodParam' must not be empty"); this.methodParam = methodParam;} @ Override protected void doFilterInternal (HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HttpServletRequest requestToUse = request If ("POST" .equals (request.getMethod ()) & & request.getAttribute (WebUtils.ERROR_EXCEPTION_ATTRIBUTE) = = null) {String paramValue = request.getParameter (this.methodParam); if (StringUtils.hasLength (paramValue)) {requestToUse = new HttpMethodRequestWrapper (request, paramValue);}} filterChain.doFilter (requestToUse, response) } / * Simple {@ link HttpServletRequest} wrapper that returns the supplied method for * {@ link HttpServletRequest#getMethod ()}. * / private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {private final String method; public HttpMethodRequestWrapper (HttpServletRequest request, String method) {super (request); this.method = method.toUpperCase (Locale.ENGLISH);} @ Override public String getMethod () {return this.method;}
With the above filter, combined with the previous method attribute of RequestMapping, you can implement a put or delete request. Let's take a look at two specific examples.
1. Take put request as an example
The first step is to configure the filter in web.xml
HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter hiddenHttpMethodFilter / *
Step 2: the definition of the form form, which is actually a post that defines a hidden field
Put request
Step 3: define the background method, define method as put, and use the annotation @ PathVariable to obtain the parameters in url.
@ RequestMapping (value = "/ testPut/ {id}", method = RequestMethod.PUT) public String testPut (@ PathVariable ("id") int id) {System.out.println ("test put function" + id); return "greeting";}
This is the implementation of a restful style put request.
The above is all the content of the article "how to achieve Restful style in Spring MVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.