7、SpringMVC之RESTful概述

打印 上一主题 下一主题

主题 885|帖子 885|积分 2655

创建名为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、页面请求示例

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/user}">测试查询所有的用户信息</a>
复制代码
7.3.2、控制器方法示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form>@RequestMapping(value = "/user",method = RequestMethod.GET)
  4. <form th:action="@{/user}" method="post">
  5.     <input type="submit" value="测试新增用户信息">
  6. </form>public String getAllUser(){
  7. <form th:action="@{/user}" method="post">
  8.     <input type="submit" value="测试新增用户信息">
  9. </form><form th:action="@{/user}" method="post">
  10.     <input type="submit" value="测试新增用户信息">
  11. </form>System.out.println("查询所有的用户信息-->/user-->GET");
  12. <form th:action="@{/user}" method="post">
  13.     <input type="submit" value="测试新增用户信息">
  14. </form><form th:action="@{/user}" method="post">
  15.     <input type="submit" value="测试新增用户信息">
  16. </form>return "success";
  17. <form th:action="@{/user}" method="post">
  18.     <input type="submit" value="测试新增用户信息">
  19. </form>}
复制代码
7.3.3、测试效果




7.4、GET请求示例(带参数)

7.4.1、页面请求示例

  1. <a th:target="_blank" href="https://www.cnblogs.com/@{/user/1}">测试查询id为1的用户信息</a>
复制代码
7.4.2、控制器方法示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form>@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
  4. <form th:action="@{/user}" method="post">
  5.     <input type="submit" value="测试新增用户信息">
  6. </form>public String getUserById(@PathVariable("id") Integer id){
  7. <form th:action="@{/user}" method="post">
  8.     <input type="submit" value="测试新增用户信息">
  9. </form><form th:action="@{/user}" method="post">
  10.     <input type="submit" value="测试新增用户信息">
  11. </form>System.out.println("根据id查询用户信息-->/user/"+id+"-->GET");
  12. <form th:action="@{/user}" method="post">
  13.     <input type="submit" value="测试新增用户信息">
  14. </form><form th:action="@{/user}" method="post">
  15.     <input type="submit" value="测试新增用户信息">
  16. </form>return "success";
  17. <form th:action="@{/user}" method="post">
  18.     <input type="submit" value="测试新增用户信息">
  19. </form>}
复制代码
7.4.3、测试效果




7.5、POST请求示例

7.5.1、页面请求示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form>
复制代码
7.5.2、控制器方法示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form>@RequestMapping(value = "/user",method = RequestMethod.POST)<form th:action="@{/user}" method="post">
  4.     <input type="submit" value="测试新增用户信息">
  5. </form>public String insertUser(){<form th:action="@{/user}" method="post">
  6.     <input type="submit" value="测试新增用户信息">
  7. </form><form th:action="@{/user}" method="post">
  8.     <input type="submit" value="测试新增用户信息">
  9. </form>System.out.println("新增用户信息-->/user-->POST");<form th:action="@{/user}" method="post">
  10.     <input type="submit" value="测试新增用户信息">
  11. </form><form th:action="@{/user}" method="post">
  12.     <input type="submit" value="测试新增用户信息">
  13. </form>return "success";<form th:action="@{/user}" method="post">
  14.     <input type="submit" value="测试新增用户信息">
  15. </form>}
复制代码
7.5.3、测试效果




7.6、配置转换请求方式的过滤器


  • 实际上,浏览器只支持发送 GET 和 POST 方式的请求;
  • SpringMVC 提供的 HiddenHttpMethodFilter 可以将 POST 请求转换为 DELETE 或 PUT 请求;
  • 使用 HiddenHttpMethodFilter 将 POST 请求转换为 DELETE 或 PUT 请求,须满足两个条件:
    a、当前请求的请求方式必须为post
    b、当前请求必须传输请求参数_method
  • 满足以上条件,HiddenHttpMethodFilter 过滤器就会将当前请求的请求方式转换为请求参数_method的值,
    因此请求参数_method的值才是最终的请求方式。

注意:HiddenHttpMethodFilter过滤器的顺序,要在CharacterEncodingFilter过滤器的后面
  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form><form th:action="@{/user}" method="post">
  4.     <input type="submit" value="测试新增用户信息">
  5. </form><form th:action="@{/user}" method="post">
  6.     <input type="submit" value="测试新增用户信息">
  7. </form><form th:action="@{/user}" method="post">
  8.     <input type="submit" value="测试新增用户信息">
  9. </form>HiddenHttpMethodFilter<form th:action="@{/user}" method="post">
  10.     <input type="submit" value="测试新增用户信息">
  11. </form><form th:action="@{/user}" method="post">
  12.     <input type="submit" value="测试新增用户信息">
  13. </form>org.springframework.web.filter.HiddenHttpMethodFilter<form th:action="@{/user}" method="post">
  14.     <input type="submit" value="测试新增用户信息">
  15. </form><form th:action="@{/user}" method="post">
  16.     <input type="submit" value="测试新增用户信息">
  17. </form><form th:action="@{/user}" method="post">
  18.     <input type="submit" value="测试新增用户信息">
  19. </form><form th:action="@{/user}" method="post">
  20.     <input type="submit" value="测试新增用户信息">
  21. </form>HiddenHttpMethodFilter<form th:action="@{/user}" method="post">
  22.     <input type="submit" value="测试新增用户信息">
  23. </form><form th:action="@{/user}" method="post">
  24.     <input type="submit" value="测试新增用户信息">
  25. </form>/*<form th:action="@{/user}" method="post">
  26.     <input type="submit" value="测试新增用户信息">
  27. </form>
复制代码
7.7、PUT请求示例

7.7.1、页面请求示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form><form th:action="@{/user}" method="post">
  4.     <input type="submit" value="测试新增用户信息">
  5. </form>
复制代码
7.7.2、控制器方法示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form>@RequestMapping(value = "/user",method = RequestMethod.PUT)<form th:action="@{/user}" method="post">
  4.     <input type="submit" value="测试新增用户信息">
  5. </form>public String updateUser(){<form th:action="@{/user}" method="post">
  6.     <input type="submit" value="测试新增用户信息">
  7. </form><form th:action="@{/user}" method="post">
  8.     <input type="submit" value="测试新增用户信息">
  9. </form>System.out.println("修改用户信息-->/user-->PUT");<form th:action="@{/user}" method="post">
  10.     <input type="submit" value="测试新增用户信息">
  11. </form><form th:action="@{/user}" method="post">
  12.     <input type="submit" value="测试新增用户信息">
  13. </form>return "success";<form th:action="@{/user}" method="post">
  14.     <input type="submit" value="测试新增用户信息">
  15. </form>}
复制代码
7.7.3、测试效果




7.8、DELETE请求示例

7.8.1、页面请求示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form><form th:action="@{/user}" method="post">
  4.     <input type="submit" value="测试新增用户信息">
  5. </form>
复制代码
7.8.2、控制器方法示例

  1. <form th:action="@{/user}" method="post">
  2.     <input type="submit" value="测试新增用户信息">
  3. </form>@RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)<form th:action="@{/user}" method="post">
  4.     <input type="submit" value="测试新增用户信息">
  5. </form>public String deleteUser(@PathVariable("id") Integer id){<form th:action="@{/user}" method="post">
  6.     <input type="submit" value="测试新增用户信息">
  7. </form><form th:action="@{/user}" method="post">
  8.     <input type="submit" value="测试新增用户信息">
  9. </form>System.out.println("根据id删除用户信息-->/user/"+id+"-->DELETE");<form th:action="@{/user}" method="post">
  10.     <input type="submit" value="测试新增用户信息">
  11. </form><form th:action="@{/user}" method="post">
  12.     <input type="submit" value="测试新增用户信息">
  13. </form>return "success";<form th:action="@{/user}" method="post">
  14.     <input type="submit" value="测试新增用户信息">
  15. </form>}
复制代码
7.8.3、测试效果




7.9、结合请求方式的派生注解

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


  • 处理get请求的派生注解-->@GetMapping
  • 处理post请求的派生注解-->@PostMapping
  • 处理put请求的派生注解-->@PutMapping
  • 处理delete请求的派生注解-->@DeleteMapping


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

民工心事

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表