SpringBoot对一个URL通过method(GET、POST、PUT、DELETE)实现增删改查操作

目录

  • [1. rest风格基础](#1. rest风格基础)
  • [2. 开启方法](#2. 开启方法)
  • [3. 实战练习](#3. 实战练习)

1. rest风格基础

我们都知道GET、POST、PUT、DELETE分别对应查、增、改、删除

虽然Postman这些工具可以直接发送GET、POST、PUT、DELETE请求。但是@RequestMapping并不支持PUT和DELETE请求操作。需要我们手动开启

2. 开启方法

PUT和DELETE还是通过method=POST进行请求,但是需要添加对应的隐藏域_method=PUT/DELETE

然后配置文件进行参数的配置:spring.mvc.hiddenmethod.filter.enabled=true

3. 实战练习

  1. 添加如下参数到application.properties文件。表示开启隐藏方法使用

    spring.mvc.hiddenmethod.filter.enabled=true

  2. 编写Controller。在一个请求路径上,分别定义了GET、POST、PUT、DELETE四种method

其中GetMapping、PostMapping、PutMapping、DeleteMapping等同于@RequestMapping对应的method。如下所示:

Java 复制代码
package com.hh.springboottest.myController;

import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {

    // @GetMapping("/user")
    @RequestMapping(value="/user", method = RequestMethod.GET)
    public String getUser() {
        return "get user";
    }

    // @PostMapping("/user")
    @RequestMapping(value="/user", method = RequestMethod.POST)
    public String saveUser() {
        return "save user";
    }

    // @PutMapping("/user")
    @RequestMapping(value="/user", method = RequestMethod.PUT)
    public String editUser() {
        return "edit user";
    }

    // @DeleteMapping("/user")
    @RequestMapping(value="/user", method = RequestMethod.DELETE)
    public String deleteUser() {
        return "delete user";
    }
}
  1. 编写resources/static/index.html页面。PUT和DELETE还是通过method=POST进行请求,但是需要添加对应的隐藏域_method=PUT/DELETE。还支持的一个隐藏域是_method=PATCH
Html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>test title</title>
</head>
<body>

<form action="/user" method="get">
    <input value="rest get提交" type="submit" />
</form>
<form action="/user" method="post">
    <input value="rest post提交" type="submit" />
</form>
<form action="/user" method="post">
    <input name="_method" type="hidden" value="PUT" />
    <input value="rest put提交" type="submit" />
</form>
<form action="/user" method="post">
    <input name="_method" type="hidden" value="DELETE" />
    <input value="rest delete提交" type="submit" />
</form>

</body>
</html>
  1. 然后访问http://localhost:8080/,点击rest get提交。如下所示:

    得到的结果如下:
相关推荐
叫我阿柒啊13 小时前
从Java全栈到前端框架:一次真实的面试对话与技术解析
java·javascript·typescript·vue·springboot·react·前端开发
虚伪的空想家15 小时前
K8S删除命名空间卡住一直Terminating状态
云原生·容器·kubernetes·删除·卡顿·delete·命名空间
荣淘淘2 天前
互联网大厂求职面试记:谢飞机的搞笑答辩
java·jvm·spring·面试·springboot·线程池·多线程
叫我阿柒啊3 天前
从全栈开发到微服务架构:一位Java工程师的实战经验分享
java·ci/cd·kafka·mybatis·vue3·springboot·fullstack
原来是好奇心3 天前
Spring Boot 事务失效的八大原因及解决方案详解
spring·springboot·事务
mask哥3 天前
DP-观察者模式代码详解
java·观察者模式·微服务·设计模式·springboot·设计原则
叫我阿柒啊4 天前
从Java全栈到前端框架:一场真实面试的深度技术探索
java·redis·微服务·typescript·vue3·springboot·jwt
awei09164 天前
SpringBoot3中使用Caffeine缓存组件
java·缓存·springboot·springboot3
麦兜*4 天前
MongoDB 事务管理:多文档操作如何保证 ACID?
java·数据库·后端·mongodb·spring cloud·springboot
叫我阿柒啊4 天前
从Java全栈开发到微服务架构:一次真实的面试实录
java·微服务·vue3·springboot·前端开发·后端开发·项目经验