Spring-REST style – source parsing
This article has been synchronized to the GitHub open source project: The Way of the Supergods in Java
What is the underlying implementation of SpringBoot’s support for RESTful requests?
To prepare
-
html
<form action="/user" method="POST"> <input name="_method" hidden value="DELETE"> <input type="submit" value="DELETE submit DELETE user information"> </form> Copy the code
-
Controller
@RequestMapping(value = "/user",method = RequestMethod.DELETE) public String deleteUser(a){ return "DELETE- DELETE user"; } Copy the code
To start the Debug
-
**_method=PUT**
<form action="/user" method="POST"> <input name="_method" hidden value="DELETE"> <input type="submit" value="DELETE submit DELETE user information"> </form> Copy the code
-
The request was intercepted by the HiddenHttpMethodFilter
// Intercept all requests @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest requestToUse = request; // Whether the request has an error and is a POST if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) { // Get an argument named 'this.methodParam' String paramValue = request.getParameter(this.methodParam); // If this parameter is present if (StringUtils.hasLength(paramValue)) { // Convert to English uppercase DELETE String method = paramValue.toUpperCase(Locale.ENGLISH); // Get the value of _method. if (ALLOWED_METHODS.contains(method)) { // If the value is allowed // Change the original request mode to _method request mode requestToUse = new HttpMethodRequestWrapper(request, method); / / packaging}}}// Release the packed request. filterChain.doFilter(requestToUse, response); } Copy the code
Through DeBug, we find that the first request comes in, the if will determine whether it is a POST request, and there is no error in the request. Then use the request.getParameter method to get the parameter named this.methodParam. We find that this. MethodParam has a value of _mothod
So we get the value of the _method parameter in the request. DELETE
String method = paramvalue.toupperCase (locale.english); Convert to uppercase. Then determine if it is included in the no yes request. With Debug, the following requests are allowed
That is, it is compatible with the following requests; The PUT. DELETE. PATCH,
An HttpMethodRequestWrapper request is then recreated.
This class supports request passing parameters. Change the native request mode to the passed parameter DELETE.
public HttpMethodRequestWrapper(HttpServletRequest request, String method) { super(request); this.method = method; } Copy the code
Release the packed request.
filterChain.doFilter(requestToUse, response); Copy the code
At this point,
Native requests change from POST method to parameter
_method
The type of request specified in It is then received by the Controller layer.
This article has been synchronized to the GitHub open source project: The Path of The Java Supergods for more Java knowledge, please visit!
Copy the code