• SpringBoot:Put和Delete请求(动力)



    目录:

    (1)设置在页面中支持Put和Delete请求

     (2)REST请求url加上请求方式必须唯一


    浏览将其只支持Get和Post请求,不支持Put、和Daelete请求的

    (1)设置在页面中支持Put和Delete请求

    MyRestController:最后一个方法

    1. package com.bjpowernode.controller;
    2. import org.springframework.web.bind.annotation.*;
    3. @RestController //@Controller 和@ResponseBody的组合
    4. public class MyRestController {
    5. //学习注解使用
    6. /*@PathVariable(路径变量):获取url中的数据
    7. * 属性:value:路径变量名
    8. * 位置:放在控制器方法形参的前面
    9. * http://localhost:9001/myboot/student/1002
    10. * */
    11. //查询id=1001的学生
    12. @GetMapping("/student/{stuId}")
    13. public String queryStudent(@PathVariable(value = "stuId") Integer studentId){
    14. return "查询学生studentId="+studentId;
    15. }
    16. /*
    17. * 创建资源Post 请求方式
    18. *
    19. * http://localhost:9001/myboot/student/zhangsan/1002
    20. * */
    21. @PostMapping("/student/{name}/{age}")
    22. public String createStudent(@PathVariable("name") String name,@PathVariable("age") Integer age){
    23. return "创建资源 student:name="+name+" age="+age;
    24. }
    25. /*
    26. * 更新资源
    27. * 当路径变量名称和形参名一样时,@PathVatiable中的value可以不写
    28. * */
    29. @PutMapping("/student/{id}/{age}")
    30. public String mnodifyStudent(@PathVariable Integer id,@PathVariable Integer age){
    31. return "更新资源,执行put请求方式:id="+id+" aeg="+age;
    32. }
    33. /*删除资源
    34. * */
    35. @DeleteMapping("/student/{id}")
    36. public String removeStudent(@PathVariable Integer id){
    37. return "删除资源,执行delete请求方式:id="+id;
    38. }
    39. @PutMapping("/student/test")
    40. //@RequestMapping(value = "/student/test",method = RequestMethod.GET)
    41. public String test(){
    42. return "执行student/test,使用的请求方式post";
    43. }
    44. }

    配置文件:application.properties

    1. server.port=9001
    2. server.servlet.context-path=/myboot

    testrest.html:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h3>测试rest请求的方式</h3>
    9. <form action="student/test" method="put">
    10. <input type="submit" value="注册学生">
    11. </form>
    12. </body>
    13. </html>

    运行主启动类:Application

    1. package com.bjpowernode;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. @SpringBootApplication
    5. public class Application {
    6. public static void main(String[] args) {
    7. SpringApplication.run(Application.class, args);
    8. }
    9. }

     点击按钮发送put请求:因为method=“post” 不支持会报400

     如果想要程序支持Put和Delete,需要做一些配置才行,框架提供了一个过滤器,过滤器负责将put和delete请求转对应的请求方式:

     application.properties:开启put,delete请求

    1. server.port=9001
    2. server.servlet.context-path=/myboot
    3. #启用put,delete请求
    4. spring.mvc.hiddenmethod.filter.enabled=true

    testrest.html:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h3>测试rest请求的方式</h3>
    9. <form action="student/test" method="post">
    10. <!--使用隐藏域-->
    11. <input type="hidden" name="_method" value="put">
    12. <input type="submit" value="put测试请求注方式">
    13. </form>
    14. <br>
    15. <form action="student/testdelete" method="post">
    16. <!--使用隐藏域-->
    17. <input type="hidden" name="_method" value="delete">
    18. <input type="submit" value="delete测试请求注方式">
    19. </form>
    20. </body>
    21. </html>

    在MyRestController:中添加一个delete方法

    1. @DeleteMapping("/student/testdelete")
    2. //@RequestMapping(value = "/student/test",method = RequestMethod.GET)
    3. public String testDelete(){
    4. return "执行student/testdelete,使用的请求方式delete";
    5. }

    点击put测试按钮:

     

    点击delete测试:

     

     (2)REST请求url加上请求方式必须唯一

    REST格式请求地址可能存在歧义,比如下方,当你在浏览器发送请求的时候,它会不知道访问那个而报错 ,分不清访问那个地址

     

     

    当你在用REST的时候了,必须保证 地址唯一:url地址+请求方式他两加在一块唯一的就行

  • 相关阅读:
    AI & Web3 盛会「EDGE」在港闭幕,融云国际影响力持续提升
    【工作笔记】缓存里的几种模式
    ABAP BP维护客户cl_md_bp_maintain=>maintain
    SpringBoot整合Kafka的快速使用教程
    怎样判断一个数是否为偶数
    Kafka + Kraft 集群搭建教程,附详细配置及自动化安装脚本
    开发知识点-人工智能-深度学习Tensorflow2.0
    当保存参数使用结构体时必备的开发技巧方式
    开juǎn有益系列(一)——Binary search(二分查找/折半查找算法)
    Kubernetes—k8s介绍
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125568713