SpringMVC中使用REST风格

打印 上一主题 下一主题

主题 1565|帖子 1565|积分 4695

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

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

x
了解REST

REST:即 Representational State Transfer。(资源)表现层状态转化。是目前最流行的一种互联网软件架构。使用这种架构的应用即为RESTFUL。它结构清晰、符合尺度、易于理解、扩展方便, 以是正得到越来越多网站的采用。
在HTTP 协议内里,四个表示利用方式的动词:GET、POST、PUT、DELETE,
它们分别对应四种根本利用:
1、GET ====== 获 取资源
  2、POST ======新建资源
  3、PUT======= 更新资源
  4、DELETE==== 删除资源
我们可以通过rest风格占位符方式,利用@PathVariable注解将占位符的值赋给调用方法参数,实现结果:
/某路径/1 HTTP GET : 得到 id = 1 的 一条数据
/某路径/1 HTTP DELETE: 删除 id = 1的 一条数据
/某路径/1   HTTP PUT: 更新id = 1的 一条数据
/某路径 HTTP POST: 新增一条数据
实现步调如下:


  • 在web.xml中设置HiddenHttpMethodFilter过滤器

    1. <!-- 前端浏览器不支持提交put、delete请求,从而要使用隐藏字段_mothd来提交,hiddenHttpMethodFilte过虑器能过_mothd的值来转换成对应的请求类型提交给Springmvc控制器,
    2.         从而 实现put、delete请求-->
    3.         <filter>
    4.                 <filter-name>hiddenHttpMethodFilte</filter-name>
    5.                 <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    6.         </filter>
    7.         <filter-mapping>
    8.                 <filter-name>hiddenHttpMethodFilte</filter-name>
    9.                 <url-pattern>/*</url-pattern>
    10.         </filter-mapping>
    复制代码

     
  • 前端请求中需要参加潜伏字段_method

    1. <%@ page language="java" contentType="text/html; charset=utf-8"
    2.     pageEncoding="utf-8"%>
    3. <!DOCTYPE html>
    4. <html>
    5. <head>
    6. <meta charset="utf-8">
    7. <title>index pag</title>
    8. </head>
    9. <body>
    10. <a href="${pageContext.request.contextPath}/testget">测试get请求</a>
    11. <form action="${pageContext.request.contextPath}/testpost" method="post">
    12. id:<input type="text" name="id" value="1"/>
    13. <button type="submit" value="提交">post提交</button>
    14. </form>
    15. <form action="${pageContext.request.contextPath}/testput" method="post">
    16. 姓名:<input type="text" name="name" value="lish"/>
    17. <input type="hidden" name="_method" value="put">
    18. <button type="submit" value="提交">put提交</button>
    19. </form>
    20. <form action="${pageContext.request.contextPath}/testdelete" method="post">
    21. 年龄:<input type="text" name="age" value="30"/>
    22. <input type="hidden" name="_method" value="delete">
    23. <button type="submit" value="提交">delete提交</button>
    24. </form>
    25. </body>
    26. </html>
    复制代码

     
  • 在控制器中使用method限定方法,使用@PathVariable获取参数

    1. /**
    2. *Description:
    3. *author: ljd
    4. *@date 2024年9月12日
    5. *@version 1.0
    6. */
    7. package test.springmvc.demo.controller;
    8. import org.springframework.stereotype.Controller;
    9. import org.springframework.web.bind.annotation.RequestMapping;
    10. import org.springframework.web.bind.annotation.RequestMethod;
    11. @Controller
    12. public class PutAndDelete {
    13.         @RequestMapping("/index")
    14.         public String index() {
    15.                 return "index";
    16.         }
    17.         @RequestMapping(value = "/testget", method = RequestMethod.GET)
    18.         public String testGet() {
    19.                 System.out.println("get...........");
    20.                 return "redirect:/index";
    21.         }
    22.         @RequestMapping(value = "/testpost", method = RequestMethod.POST)
    23.         public String testPost(int id) {
    24.                 System.out.println("post...........");
    25.                 System.out.println("id:" + id);
    26.                 return "redirect:/index";
    27.         }
    28.         @RequestMapping(value = "/testput", method = RequestMethod.PUT)
    29.         public String testPut(String name) {
    30.                 System.out.println("put...........");
    31.                 System.out.println("name:" + name);
    32.                 return "redirect:/index";
    33.         }
    34.         @RequestMapping(value = "/testdelete", method = RequestMethod.DELETE)
    35.         public String testDelete(Integer age) {
    36.                 System.out.println("delete...........");
    37.                 System.out.println("age:" + age);
    38.                 return "redirect:/index";
    39.         }
    40. }
    复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

风雨同行

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表