Introduction
REST is the Representational State Transfer. (Resource) Performance layer state transformation. It is the most popular Internet software architecture at present. It has clear structure, compliant with standards, easy to understand and easy to expand, so it is being adopted by more and more websites, POST, DELETE, PUT, and GET correspond to CRUD respectively. Spring3.0 began to support REST-style requests, converting POST requests into PUT and DELETE requests through org.springframework.web.filter.HiddenHttpMethodFilter. This experiment uses Spring 4.0.
HiddenHttpMethodFilter Source Code
public static final String DEFAULT_METHOD_PARAM = "_method";@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {String paramValue = request.getParameter(this.methodParam);if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {String method = paramValue.toUpperCase(Locale.ENGLISH);HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);filterChain.doFilter(wrapper, response);} else {filterChain.doFilter(request, response);}}From the source code of HiddenHttpMethodFilter, it can be seen that Spring converts it according to the _method parameter in the request. Therefore, if you want to initiate a REST-style DELETE or PUT request, you only need to include the _method parameter in the form and set the value of _method to DELETE or PUT (caps). Detailed examples are as follows:
Configure HiddenHttpMethodFilter in web.xml
Write handler code writing page
<!-- Configure org.springframework.web.filter.HiddenHttpMethodFilter to convert POST requests into PUT or DELETE requests --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
package rex.springmvc.handlers;import org.apache.log4j.Logger;import org.springframework.steretype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;@RequestMapping(value="/restTest")@Controllerpublic class RestTestHandler {private static final Logger logger = Logger.getLogger(RestTestHandler.class);private static final String SUCCESS = "success";@RequestMapping(value="/restGet/{id}", method=RequestMethod.GET) public String restGet(@RequestParam(value="id", required=false) Integer id){logger.debug("restGet:" + id);return SUCCESS;}@RequestMapping(value="/restPut/{id}", method=RequestMethod.PUT) public String restPut(@RequestParam(value="id", required=false) Integer id){logger.debug("restPut:" + id);return SUCCESS;}@RequestMapping(value="/restDelete/{id}", method=RequestMethod.DELETE) public String restDelete(@RequestParam(value="id", required=false) Integer id){logger.debug("restDelete:" + id);return SUCCESS;}@RequestMapping(value="/restPost", method=RequestMethod.POST) public String restPost(){logger.debug("restPost");return SUCCESS;}}<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Rest Test</title></head><body> <br> <a href="restTest/restGet/1" rel="external nofollow" >Test Rest Get</a> <br> <form action="restTest/restPut/1" method="post"> <input type="hidden" name="_method" value="PUT"> <input type="submit" value="submit"> </form> <br> <form action="restTest/restDelete/1" method="post"> <input type="hidden" name="_method" value="DELETE"> <input type="submit" value="submit"> </form> <br> <br> <form action="restTest/restPost" method="post"> <input type="submit" value="submit"> </form></body></html>
Note: The @RequestParam annotation in the handler must be added with the required parameter, otherwise a 400 error will occur when accessing the page.
Summarize
The above is all the content of this article about the introduction of springmvc Rest style and implementation code examples. I hope it will be helpful to everyone. Interested friends can continue to refer to this site:
SpringMVC Getting Started Example
Detailed explanation of user query code for SpringMVC development restful API
Introduction to SpringMVC's method of implementing asynchronous upload using MultipartFile
If there are any shortcomings, please leave a message to point it out.