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

实现步骤如下:

在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>

前端请求中需要加入隐藏字段_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>

在控制器中使用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";
   	}

   }
相关推荐
垚森12 天前
SpringBoot兼容SpringMVC带有.do后缀的请求
springmvc·.do后缀
_leyilea12 天前
Java内存马系列 | SpringMVC内存马 - 上 | SpringMVC代码分析
java·开发语言·springmvc·内存马
沿途欣赏i15 天前
关于SpringMVC的理解
springmvc
m0_7194145617 天前
【SpringMVC】
java·springmvc
王小二(海阔天空)22 天前
浅谈SpringMvc的核心流程与组件
springmvc·核心流程·核心组件
S-X-S24 天前
SpringMVC核心机制环境搭建
springmvc
S-X-S1 个月前
自定义@ResponseBody以及SpringMVC总结
springmvc
空谷忧人1 个月前
【SpringMVC】SpringMVC中单元方法获取请求数据的方式
java·后端·mvc·springmvc
醉颜凉1 个月前
SpringMVC 运行流程
java·面试·mvc·springmvc·model·controller·view