欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > SpringMVC中使用REST风格

SpringMVC中使用REST风格

2024/10/26 7:15:31 来源:https://blog.csdn.net/jingde528/article/details/142168387  浏览:    关键词:SpringMVC中使用REST风格

了解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: 新增一条数据

实现步骤如下:

  1. 在web.xml中配置HiddenHttpMethodFilter过滤器

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

     
  2. 前端请求中需要加入隐藏字段_method

    <%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>index pag</title>
    </head>
    <body>
    <a href="${pageContext.request.contextPath}/testget">测试get请求</a><form action="${pageContext.request.contextPath}/testpost" method="post">
    id:<input type="text" name="id" value="1"/>
    <button type="submit" value="提交">post提交</button>
    </form><form action="${pageContext.request.contextPath}/testput" method="post">
    姓名:<input type="text" name="name" value="lish"/>
    <input type="hidden" name="_method" value="put">
    <button type="submit" value="提交">put提交</button>
    </form><form action="${pageContext.request.contextPath}/testdelete" method="post">
    年龄:<input type="text" name="age" value="30"/>
    <input type="hidden" name="_method" value="delete">
    <button type="submit" value="提交">delete提交</button>
    </form></body>
    </html>

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

    /**
    *Description:
    *author: ljd
    *@date 2024年9月12日 
    *@version 1.0 
    */
    package test.springmvc.demo.controller;import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;@Controller
    public class PutAndDelete {@RequestMapping("/index")public String index() {return "index";}@RequestMapping(value = "/testget", method = RequestMethod.GET)public String testGet() {System.out.println("get...........");return "redirect:/index";}@RequestMapping(value = "/testpost", method = RequestMethod.POST)public String testPost(int id) {System.out.println("post...........");System.out.println("id:" + id);return "redirect:/index";}@RequestMapping(value = "/testput", method = RequestMethod.PUT)public String testPut(String name) {System.out.println("put...........");System.out.println("name:" + name);return "redirect:/index";}@RequestMapping(value = "/testdelete", method = RequestMethod.DELETE)public String testDelete(Integer age) {System.out.println("delete...........");System.out.println("age:" + age);return "redirect:/index";}}
    

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com