民工心事 发表于 2023-11-9 20:41:39

7、SpringMVC之RESTful概述

创建名为spring_mvc_rest的新module,过程参考5.2节和6.6节
7.1、简介


[*]RESTful 也称为REST(英文:Representational State Transfer)即表现层状态传递,它是一种软件架构风格或设计风格;
[*]REST 是 Roy Fielding 博士( HTTP 规范的主要编写者之一)在其2000年的博士论文中提出来的;
[*]其作用是,降低开发的复杂性、提高系统的可伸缩性;
[*]通过基于 REST 的 API 公开系统资源是一种灵活的方法,可以为不同种类的应用程序提供以标准方式格式化的数据;
[*]Ajax 与 REST 之间的完美配合增加了当今人们对 REST 的注意力。
7.2、传统风格与RESTful风格


[*]RESTful 风格提倡 URL 地址使用统一的风格设计,各单词之间用斜杠分开;
[*]RESTful 风格不使用问号键值对的方式携带请求参数,而是将要发送给服务器的数据作为 URL 地址的一部分;
[*]RESTful 风格中不存在动词形式的路径,如updateUser表示修改用户,是一个动词,要改为名词user;
[*]RESTful 风格中的路径只存在资源名称,增删改查的操作则是通过 HTTP 的请求方式来体现;
[*]HTTP 协议里面,有四个表示操作方式的动词:GET、POST、PUT、DELETE;
[*]它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE用来删除资源。
操作传统风格REST风格新增操作saveUseruser-->post请求方式删除操作deleteUser?id=1user/1-->delete请求方式修改操作updateUseruser-->put请求方式查询操作getUserById?id=1user/1-->get请求方式7.3、GET请求示例(不带参数)

7.3.1、页面请求示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019142341071-560748727.png
<a th:target="_blank" href="https://www.cnblogs.com/@{/user}">测试查询所有的用户信息</a>7.3.2、控制器方法示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019143408950-1309330532.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>@RequestMapping(value = "/user",method = RequestMethod.GET)
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>public String getAllUser(){
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>System.out.println("查询所有的用户信息-->/user-->GET");
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>return "success";
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>}7.3.3、测试效果

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019143815739-744784511.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019143844348-786153344.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019143932613-1715165470.png
7.4、GET请求示例(带参数)

7.4.1、页面请求示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019144314366-1640447576.png
<a th:target="_blank" href="https://www.cnblogs.com/@{/user/1}">测试查询id为1的用户信息</a>7.4.2、控制器方法示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019145122028-642895543.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>public String getUserById(@PathVariable("id") Integer id){
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>System.out.println("根据id查询用户信息-->/user/"+id+"-->GET");
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>return "success";
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>}7.4.3、测试效果

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019145312692-1127265277.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019145326917-1657446149.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019145348077-1725356564.png
7.5、POST请求示例

7.5.1、页面请求示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019150117500-2108728741.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>7.5.2、控制器方法示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019150526110-846002920.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>@RequestMapping(value = "/user",method = RequestMethod.POST)<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>public String insertUser(){<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>System.out.println("新增用户信息-->/user-->POST");<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>return "success";<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>}7.5.3、测试效果

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019150719648-20800652.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019150802868-83774546.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019150825721-1056342335.png
7.6、配置转换请求方式的过滤器


[*]实际上,浏览器只支持发送 GET 和 POST 方式的请求;
[*]SpringMVC 提供的 HiddenHttpMethodFilter 可以将 POST 请求转换为 DELETE 或 PUT 请求;
[*]使用 HiddenHttpMethodFilter 将 POST 请求转换为 DELETE 或 PUT 请求,须满足两个条件:
a、当前请求的请求方式必须为post
b、当前请求必须传输请求参数_method
[*]满足以上条件,HiddenHttpMethodFilter 过滤器就会将当前请求的请求方式转换为请求参数_method的值,
因此请求参数_method的值才是最终的请求方式。
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019163141312-1332499696.png
注意:HiddenHttpMethodFilter过滤器的顺序,要在CharacterEncodingFilter过滤器的后面
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>HiddenHttpMethodFilter<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>org.springframework.web.filter.HiddenHttpMethodFilter<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>HiddenHttpMethodFilter<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>/*<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>7.7、PUT请求示例

7.7.1、页面请求示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019153227562-1665880726.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>7.7.2、控制器方法示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154124291-233015129.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>@RequestMapping(value = "/user",method = RequestMethod.PUT)<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>public String updateUser(){<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>System.out.println("修改用户信息-->/user-->PUT");<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>return "success";<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>}7.7.3、测试效果

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019153505061-554042726.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019153520817-1638073494.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154241406-1158033426.png
7.8、DELETE请求示例

7.8.1、页面请求示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154802485-1986985195.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>7.8.2、控制器方法示例

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154625358-697952318.png
<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>public String deleteUser(@PathVariable("id") Integer id){<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>System.out.println("根据id删除用户信息-->/user/"+id+"-->DELETE");<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form><form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>return "success";<form th:action="@{/user}" method="post">
    <input type="submit" value="测试新增用户信息">
</form>}7.8.3、测试效果

https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154847909-1761016987.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154901740-1398810435.png
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019154928612-1179844508.png
7.9、结合请求方式的派生注解

对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解


[*]处理get请求的派生注解-->@GetMapping
[*]处理post请求的派生注解-->@PostMapping
[*]处理put请求的派生注解-->@PutMapping
[*]处理delete请求的派生注解-->@DeleteMapping
https://img2023.cnblogs.com/blog/2052479/202310/2052479-20231019165259082-1036665730.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 7、SpringMVC之RESTful概述