1. Spring MVC 中使用 RESTFul 编程风格
@
2. RESTFul 编程风格
2.1 RESTFul 是什么
RESTFul 是 web 服务器接口 的一种设计风格。
RESTFul 定义了一组约束条件和规范,可以让 web服务器接口 更加简洁,易于理解,易于扩展,安全可靠。
RESTFUl 对一个 web 服务器接口 都规定了哪些东西 ?
- 对请求的 URL 格式有约束和规范
- 对 HTTP 的请求方式有约束和规范
- 对请求和响应的数据格式有约束和规范
- 对 HTTP 状态码有约束和规范
- 等......
REST 对请求方式的约束是这样的:
- 查询必须发送 GET 请求
- 新增必须发送 POST 请求
- 修改必须发送 PUT 请求
- 删除必须发送 DELETE 请求
REST对 URL 的约束时这样的:
- 传统的 URL : get 请求,/springmvc/getUserById?id=1
- REST风格的 URL:get 请求,/springmvc/user/1
- 传统的URL :get 请求,/springmvc/deleteUserById?id=1
- REST风格的URL:delete 请求,/springmvc/user/1
RESTFul 对 URL 的约束和规范的核心时:通过采用 :不同的请求方式 + URL 来确定 web 服务中的资源。
RESTFul 的英文全称时:Representational State Transfer(表述性状态转移),简称 REST。
表述性(Representational) 是:URL + 请求方式。
状态(State) 是 :服务器端的数据。
转移(Transfer) 是:变化。
表述性转移是指:通过 URL + 请求方式来控制服务器端数据的变化。
2.2 RESTFul风格与传统方式对比
统的 URL 与 RESTful URL 的区别是传统的 URL 是基于方法名进行资源访问和操作,而 RESTful URL 是基于资源的结构和状态进行操作的。下面是一张表格,展示两者之间的具体区别:
| 传统的 URL | RESTful URL |
|---|---|
| GET /getUserById?id=1 | GET /user/1 |
| GET /getAllUser | GET /user |
| POST /addUser | POST /user |
| POST /modifyUser | PUT /user |
| GET /deleteUserById?id=1 | DELETE /user/1 |
从上表中我们可以看出,传统的 URL是基于动作的,而 RESTful URL 是基于资源和状态的,因此 RESTful URL 更加清晰和易于理解,这也是 REST 架构风格被广泛使用的主要原因之一。
3. Spring MVC 中使用 RESTFul 编程风格(增删改查)的使用
3.1 准备工作
导入相关依赖 jar 包。


<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.rainbowseagroupId>
<artifactId>springmvc-007artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>6.1.4version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.5.3version>
dependency>
<dependency>
<groupId>jakarta.servletgroupId>
<artifactId>jakarta.servlet-apiartifactId>
<version>6.0.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring6artifactId>
<version>3.1.2.RELEASEversion>
dependency>
dependencies>
project>
相关包 / 目录的创建,配置。

springmvc.xml 配置文件的配置;

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.rainbowsea.springmvc.controller">context:component-scan>
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8"/>
<property name="order" value="1"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring6.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML"/>
<property name="characterEncoding" value="UTF-8"/>
bean>
property>
bean>
property>
bean>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:view-controller path="/" view-name="index">mvc:view-controller>
beans>
3.2 RESTFul 风格的 “查询” 所有(RESTFul 规范 需要发送 GET请求)
RESTFul 规范中规定,如果要查询数据,需要发送 GET 请求。

html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1>测试 RESTFul 编程风格h1>
<hr>
<a th:href="@{/user}">查看用户列表a> <br>
body>
html>
控制器 Controller:

import com.rainbowsea.springmvc.bean.User;
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 // 交给 Spring IOC 容器进行管理
public class UserController {
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getAll() {
System.out.println("正在查询所有用户信息...");
return "ok";
}
}
ok 的页面视图:

html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>OK页面title>
head>
<body>
<h1>OK !h1>
body>
html>
启动服务器,测试:http://localhost:8080/springmvc



3.3 RESTFul 风格的 根据 “id 查询”( RESTFul 规范 需要发送 GET请求)
RESTFul 规范中规定,如果要查询数据,需要发送GET请求。
首页index.html

html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1>测试 RESTFul 编程风格h1>
<hr>
<a th:href="@{/user}">查看用户列表a> <br>
<a th:href="@{/user/1}">查询id=1的这个用户信息a><br>
body>
html>
控制器 Controller:

import com.rainbowsea.springmvc.bean.User;
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 // 交给 Spring IOC 容器进行管理
public class UserController {
//@RequestMapping(value = "/user/{占位符}",method = RequestMethod.GET)
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String getById(@PathVariable(value = "id") String id) {
System.out.println("正在根据用户 id 查询用户信息...用户 id 是" + id);
return "ok";
}
}
启动服务器测试:



3.4 RESTFul 风格的 “增加数据” (RESTFul 规范 需要发送 POST 请求)
RESTFul规范中规定,如果要进行保存操作,需要发送POST请求。
这里我们添加一个 User Bean 类,用于作为对象进行存储。

package com.rainbowsea.springmvc.bean;
public class User {
private String username;
private String password;
private Integer age;
public User() {
}
public User(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
页面编写:

html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1>测试 RESTFul 编程风格h1>
<hr>
<a th:href="@{/user}">查看用户列表a> <br>
<a th:href="@{/user/1}">查询id=1的这个用户信息a><br>
<form th:action="@{/user}" method="post">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="保存">
form>
<hr>
body>
html>
控制器 Controller:

启动服务器测试:


3.5 RESTFul 风格的 “修改数据” (RESTFul 规范 需要发送 PUT 请求)
RESTFul规范中规定,如果要进行保存操作,需要发送PUT请求。
如何发送PUT请求?
第一步:首先你必须是一个POST请求。
第二步:在发送POST请求的时候,提交这样的数据:_method=PUT,使用隐藏域进行配置
第三步:在web.xml文件配置SpringMVC提供的过滤器:HiddenHttpMethodFilter
注意:
<input type="hidden" name="_method" value="put"> 隐藏域的 name 必须只能是 “_method”, value是 put(大小写忽略)
第一步:首先你必须是一个POST请求。
第二步:在发送POST请求的时候,提交这样的数据:_method=PUT

<h2>修改h2>
<form th:action="@{/user}" method="post">
<input type="hidden" name="_method" value="put">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="修改">
form>

第三步:在web.xml文件配置SpringMVC提供的过滤器:HiddenHttpMethodFilter
注意:该过滤器一定要在字符编码过滤器后面配置,不然,先设置的话,可能会出现获取到的请求数据是乱码

<filter>
<filter-name>HiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
页面编写:

html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1>测试 RESTFul 编程风格h1>
<hr>
<a th:href="@{/user}">查看用户列表a> <br>
<a th:href="@{/user/1}">查询id=1的这个用户信息a><br>
<form th:action="@{/user}" method="post">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="保存">
form>
<hr>
<h2>修改h2>
<form th:action="@{/user}" method="post">
<input type="hidden" name="_method" value="put">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="修改">
form>
body>
html>
控制器 Controller:

启动服务器测试:


3.6 RESTFul 风格的 “删除数据” 数据(RESTFul 规范 需要发送 DELETE 请求)
RESTFul规范中规定,如果要进行 删除 操作,需要发送DELETE 请求。
如何发送 DELETE 请求?,和 发送 PUT 请求的三步是一样的,只需要将 value 的值改为 delete 即可
第一步:首先你必须是一个POST请求。
第二步:在发送POST请求的时候,提交这样的数据:_method=PUT,使用隐藏域进行配置
第三步:在web.xml文件配置SpringMVC提供的过滤器:HiddenHttpMethodFilter
注意:
<input type="hidden" name="_method" value="delete"> 隐藏域的 name 必须只能是 “_method”, value是 delete (大小写忽略)
页面编写:

<hr>
<h2>删除用户h2>
<a th:href="@{user/120}" onclick="del(event)">删除用户id = 120 的用户信息a>
<form id="delForm" method="post">
<input type="hidden" name="_method" value="delete">
form>
<script>
function del(event) {
// 获取表单
let delForm = document.getElementById("delForm");
// 给 form 的 action 赋值
delForm.action = event.target.href;
// 发送POST 请求提交表单
delForm.submit();
// 非常重要,你需要阻止超链接的默认行为
event.preventDefault();
}
script>
html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页title>
head>
<body>
<h1>测试 RESTFul 编程风格h1>
<hr>
<a th:href="@{/user}">查看用户列表a> <br>
<a th:href="@{/user/1}">查询id=1的这个用户信息a><br>
<form th:action="@{/user}" method="post">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="保存">
form>
<hr>
<h2>修改h2>
<form th:action="@{/user}" method="post">
<input type="hidden" name="_method" value="put">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
年龄: <input type="text" name="age"><br>
<input type="submit" value="修改">
form>
<hr>
<h2>删除用户h2>
<a th:href="@{user/120}" onclick="del(event)">删除用户id = 120 的用户信息a>
<form id="delForm" method="post">
<input type="hidden" name="_method" value="delete">
form>
<script>
function del(event) {
// 获取表单
let delForm = document.getElementById("delForm");
// 给 form 的 action 赋值
delForm.action = event.target.href;
// 发送POST 请求提交表单
delForm.submit();
// 非常重要,你需要阻止超链接的默认行为
event.preventDefault();
}
script>
body>
html>
控制器 Controller:

package com.rainbowsea.springmvc.controller;
import com.rainbowsea.springmvc.bean.User;
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 // 交给 Spring IOC 容器进行管理
public class UserController {
//@RequestMapping(value = "/user/{占位符}",method = RequestMethod.GET)
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String getById(@PathVariable(value = "id") String id) {
System.out.println("正在根据用户 id 查询用户信息...用户 id 是" + id);
return "ok";
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String getAll() {
System.out.println("正在查询所有用户信息...");
return "ok";
}
@RequestMapping(value = "/user", method = RequestMethod.POST)
public String save(User user) {
System.out.println("正在保存用户信息");
System.out.println(user);
return "ok";
}
@RequestMapping(value = "/user", method = RequestMethod.PUT)
public String modify(User user) {
System.out.println("正在修改用户信息" + user);
return "ok";
}
@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
public String del(@PathVariable(value = "id") String id) {
System.out.println("正删除用户 : " + id);
return "ok";
}
}
启动服务器测试:


4. 补充: HiddenHttpMethodFilter 过滤器源码说明
HiddenHttpMethodFilter是Spring MVC框架提供的,专门用于RESTFul编程风格。
实现原理可以通过源码查看:



通过源码可以看到,if语句中,首先判断是否为POST请求,如果是POST请求,调用request.getParameter(this.methodParam)。可以看到this.methodParam是_method,这样就要求我们在提交请求方式的时候必须采用这个格式:_method=put。获取到请求方式之后,调用了toUpperCase转换成大写了。因此前端页面中小写的put或者大写的PUT都是可以的。if语句中嵌套的if语句说的是,只有请求方式是 PUT,DELETE,PATCH的时候会创建HttpMethodRequestWrapper对象。而HttpMethodRequestWrapper对象的构造方法是这样的:

这样method就从POST变成了:PUT/DELETE/PATCH。
重点注意事项:CharacterEncodingFilter和HiddenHttpMethodFilter的顺序
细心的同学应该注意到了,在HiddenHttpMethodFilter源码中有这样一行代码:

)
大家是否还记得,字符编码过滤器执行之前不能调用 request.getParameter方法,如果提前调用了,乱码问题就无法解决了。因为request.setCharacterEncoding()方法的执行必须在所有request.getParameter()方法之前执行。因此这两个过滤器就有先后顺序的要求,在web.xml文件中,应该先配置CharacterEncodingFilter,然后再配置HiddenHttpMethodFilter。
5. 总结:
RESTFul风格与传统方式对比区别
RESTFul 风格的 “查询” 所有(RESTFul 规范 需要发送 GET请求)
RESTFul 风格的 根据 “id 查询”( RESTFul 规范 需要发送 GET请求)
RESTFul 风格的 “增加数据” (RESTFul 规范 需要发送 POST 请求)
RESTFul 风格的 “修改数据” (RESTFul 规范 需要发送 PUT 请求)
如何发送PUT请求?
第一步:首先你必须是一个POST请求。
第二步:在发送POST请求的时候,提交这样的数据:_method=PUT,使用隐藏域进行配置
第三步:在web.xml文件配置SpringMVC提供的过滤器:HiddenHttpMethodFilter**
注意:
<input type="hidden" name="_method" value="put"> 隐藏域的 name 必须只能是 “_method”, value是 put(大小写忽略)
RESTFul 风格的 “删除数据” 数据(RESTFul 规范 需要发送 DELETE 请求);如何发送 DELETE 请求?,和 发送 PUT 请求的三步是一样的,只需要将 value 的值改为 delete 即可
HiddenHttpMethodFilter 该过滤器一定要在字符编码过滤器后面配置,不然,先设置的话,可能会出现获取到的请求数据是乱码。
6. 最后:
“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的领域奋斗。感谢你们,我们总会在某个时刻再次相遇。”



