表单提交RESTful风格
表单提交RESTful风格——GET,POST,DELETE,PUT 4种方式处理源码
在REST中,资源师通过URI来指定的,对资源的增删改查操作可以通过HTTP协议提供的GET\POST\PUT\DELETE等方法实现。
表单的请求只能是GET或者POST,而REST中的DELETE和PUT请求需要开启配置后再设置隐藏请求方式才能达到目的,我们在后续的学习工作中很少用到这种方式,后边大多是前后端分离项目,客户端发出的请求可以直接通过DELETE或者PUT去请求。
1. RESTful手动开启配置生效——application.properties
开启配置后,表单提交POST中含隐藏方法的会被拦截,拦截后读取POST提交表单中隐藏方法_method的值,不开启配置会直接按照POST请求处理。
spring.mvc.hiddenmethod.filter.enabled=true
2. 表单提交REST原理:
- 表单提交会带上_method=PUT
- 请求过来被HiddenHttpMethodFilter拦截。
- POST请求,并且请求是正常。
- 获取到_method的值。
- 兼容以下请求;PUT、DELETE、PATCH
- 原生request (post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值,
- 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的_method值。
3. REST使用客户端工具:
- 如PostMan直接发送Put、delete等方式请求,无需Filter。所以也无需开启配置。
4. 源码查看小技巧
4.1 CTRL+H——类的层次结构
在选中所需要查看层次结构类的前提下,按快捷键: CTRL+H,可以进行查看。
4.2 CTRL+ALT+U——类的继承和实现关系图
在选中所需要查看层次结构类的前提下,按快捷键: CTRL+ALT+U
或 CTRL+ALT+SHIFT+U
,可以进行查看继承类和实现接口的关系图。
5. 测试
创建一个SpringBoot项目,用表单和PostMan两种方式去请求接口测试
- 引入相关依赖——pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-REST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 核心配置 ——application.properties
spring.mvc.hiddenmethod.filter.enabled=true
- 控制层——UserController.java
package com.guo.springboot.controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
public class UserController {
@GetMapping("/user")
public String getUser(HttpServletRequest request){
String username = request.getParameter("username");
return "1"+username;
}
@PostMapping("/user")
public String postUser(String username){
return "2"+username;
}
@DeleteMapping("/user")
public String deleteUser(String username){
return "3"+username;
}
@PutMapping("/user")
public String putUser(String username){
return "4"+ username;
}
}
- 创建首页index.html
首页默认放在resources→static下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>测试表单提交REST的四种请求</h1>
<form action="/user" method="get">
<input name="username" value="REST-GET" type="submit">
</form>
<form action="/user" method="post">
<input name="username" value="REST-POST" type="submit">
</form>
<form action="/user" method="post">
<input type="hidden" name="_method" value="delete" >
<input name="username" value="REST-DELETE" type="submit">
</form>
<form action="/user" method="post">
<input type="hidden" name="_method" value="put" >
<input name="username" value="REST-PUT" type="submit">
</form>
</body>
</html>
- 启动程序
5.1 表单提交REST测试
RESTful手动开启配置
spring.mvc.hiddenmethod.filter.enabled=true
首页:http://localhost:8080/
1.点击第一个GET提交表单
2.点击第二个POST提交表单
3.点击第三个DELETE提交表单
4.点击第四个PUT提交表单
5.2 客户端PostMan测试
RESTful手动关闭配置(默认就是关闭的,这里可以直接删除这条配置),这里的开启和关闭只会影响表单的提交DELETE和PUT请求,这两种请求是放在POST请求表单中的隐藏方法中,开启配置的时候请求拦截POST请求,读取隐藏的方法,把隐藏的方法替代POST请求,进而达到“偷梁换柱”的目的;
而关闭配置就默认按照POST请求发起请求。
spring.mvc.hiddenmethod.filter.enabled=false
关闭后POST表单中的隐藏DELETE和PUT请求直接走POST请求 2 了