REST style request method

REST: Representational State Transfer. (Resource) State transition of presentation layer . It is currently the most popular Internet software architecture. It has a clear structure, conforms to standards, is easy to understand, and easy to expand, so it is being adopted by more and more websites. Using the REST style request method can simplify the url and achieve different methods using the same url and different request methods.

The REST-style request method corresponds to the following four requests, and these four requests correspond to four operations on resources:

GET -----------> Get resources

POST ---------> New resource

PUT -----------> Update resources

DELETE ------> delete resource

GET

We are all familiar with GET requests, such as direct access to the address bar and hyperlink access.

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 --- 查询数据 --- " + id);
		return "success";
	}

}

And use a hyperlink to issue 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风格请求</title>
</head>
<body>
	<a href="rest/1">GET 请求</a>
</body>
</html>

Visit the page, click the hyperlink on the page, you will find that the screen jumps to success.jsp and "GET --- Query Data --- 1" is output on the console. It means that the controller successfully received the GET request and obtained the parameters in the url address.

POST

The most common form of POST request is the 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() {
		// 接收表单中的各种信息
		System.out.println("POST --- 创建数据");
		return "success";
	}

}

And use a form to issue 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 请求" />
	</form>
</body>
</html>

Visit the page, click the button in the form on the page, you will find that the screen jumps to success.jsp and the output "POST --- Create data" on the console. It means that the controller successfully received the GET request.

PUT 和 DELETE

Creating a GET request and a PUT request are simple, but what about PUT and DELETE requests? In our normal access, PUT requests and DELETE requests cannot be created, so we need to use the filter of one of Spring's three major components Filterto issue PUT requests and DELTE requests.

Steps to send PUT and DELTE requests:

  1. Add a HiddenHttpMethodFilterfilter in 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>
  1. Create a form with a POST request:
	<form action="rest" method="post">
		<input type="submit" value="PUT 请求" />
	</form>
  1. Add a tag with name _method in the form :
	<form action="rest" method="post">
		<input type="hidden" name="_method" value="put" />
		<input type="submit" value="PUT 请求" />
	</form>

The DELTE request is the same, just change the put request to delte. not case sensitive.

If after following the above steps, click the button to find the HTTP 405 error message : "Message JSP only allows GET, POST or HEAD . Jasper also allows OPTIONS" . So please refer to HTTP 405 的错误提示:消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS 的解决方法the section.

Guess you like

Origin www.cnblogs.com/lemon-coke-pudding/p/12728000.html