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";
   	}

   }
相关推荐
我登哥MVP6 天前
【SpringMVC笔记】 - 2 - @RequestMapping
java·spring boot·spring·servlet·tomcat·intellij-idea·springmvc
我登哥MVP7 天前
【SpringMVC笔记】 - 1 - SpringMVC入门
java·spring boot·spring·tomcat·maven·intellij-idea·springmvc
tryxr9 天前
SpringMVC 中的常用注解和用法
spring·mvc·springmvc
cheems952711 天前
[SpringMVC] SpringWebMVC常见注解介绍
java·springmvc·注解
NGC_66111 个月前
详细解析SpringMVC:原理、架构与实战核心
springmvc
惊讶的猫1 个月前
SpringMVC介绍
java·springmvc·springboot
Zsh-cs3 个月前
苍穹外卖之SpringMVC的消息转换器在项目中的应用场景
springmvc·苍穹外卖·消息转换器
这周也會开心3 个月前
SSM 配置 index 页面的实现方式
java·tomcat·springmvc
海南java第二人3 个月前
Spring MVC核心流程深度解析:从请求到响应的完美掌控
java·springmvc
BD_Marathon3 个月前
RESTful快速开发
springmvc