REST: representative state transfer. (resources) state transformation of presentation layer. It is the most popular Internet software architecture. It has a clear structure, conforms to the standard, is easy to understand, and is easy to expand, so it is being adopted by more and more websites. Using the REST style of request mode can simplify the url, so that different request modes of the same url can be used to execute different methods.
The REST style requests correspond to the following four types of requests, one of which corresponds to four types of resource operations:
Get -------- > get resources
Post - > new resource
Put -------- > Update resources
Delete -------- delete resource
GET
We are familiar with GET requests, such as direct access to the address bar, hyperlink access, etc.
We create a controller to receive GET requests:
package com.pudding.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class RestController { @RequestMapping(value = "/rest/{id}", method = RequestMethod.GET) public String get(@PathVariable Integer id) { System.out.println("GET --- Query data --- " + id); return "success"; } }
And use a hyperlink to make a GET request:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Rest Style request</title> </head> <body> <a href="rest/1">GET request</a> </body> </html>
Visit the page, click the hyperlink on the page, and you will find that the screen jumps to success.jsp and outputs "GET - query data - 1" on the console. It indicates that the controller successfully received the GET request and obtained the parameters in the url address.
POST
The most common way of POST request is form form.
We create a controller to receive POST requests:
package com.pudding.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class RestController { @RequestMapping(value = "/rest", method = RequestMethod.POST) public String post() { // Receive various information in the form System.out.println("POST --- Create data"); return "success"; } }
And use a form form to make a POST request:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="rest" method="post"> <input type="submit" value="POST request" /> </form> </body> </html>
Visit the page, click the button in the form on the page, and you will find that the screen jumps to success.jsp and outputs "POST - create data" on the console. The controller successfully received the GET request.
PUT and DELETE
It's easy to create GET requests and PUT requests, but what about PUT requests and DELETE requests? In our normal access, we can't create PUT requests and DELETE requests, so we need to use the Filter filter, one of Spring's three major components, to issue PUT requests and DELETE requests.
To send PUT and DELTE requests:
- Add HiddenHttpMethodFilter to web.xml:
<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>
- Create a form form with a POST request:
<form action="rest" method="post"> <input type="submit" value="PUT request" /> </form>
- Add a label with the name "Ou method" to the form:
<form action="rest" method="post"> <input type="hidden" name="_method" value="put" /> <input type="submit" value="PUT request" /> </form>
The same is true for a delte request. You only need to change the put request to a delte request. Case insensitive.
If you follow the above steps, click the button to find the error prompt of HTTP 405: "message JSP only allows GET, POST or HEAD. Jasper also allows OPTIONS. ". Please refer to the error prompt of HTTP 405: the message JSP only allows GET, POST or HEAD. Jasper also allows the solutions section of OPTIONS.