目录:

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

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


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

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

MyRestController:最后一个方法

package com.bjpowernode.controller;

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

@RestController   //@Controller 和@ResponseBody的组合
public class MyRestController {
    //学习注解使用

    /*@PathVariable(路径变量):获取url中的数据
    *     属性:value:路径变量名
    *      位置:放在控制器方法形参的前面
    * http://localhost:9001/myboot/student/1002
    * */
    //查询id=1001的学生
    @GetMapping("/student/{stuId}")
    public String queryStudent(@PathVariable(value = "stuId") Integer studentId){
        return "查询学生studentId="+studentId;
    }

    /*
    * 创建资源Post 请求方式
    *
    * http://localhost:9001/myboot/student/zhangsan/1002
    * */
    @PostMapping("/student/{name}/{age}")
    public String createStudent(@PathVariable("name") String name,@PathVariable("age") Integer age){
        return "创建资源 student:name="+name+" age="+age;
    }

    /*
    * 更新资源
    *  当路径变量名称和形参名一样时,@PathVatiable中的value可以不写
    * */
    @PutMapping("/student/{id}/{age}")
    public String mnodifyStudent(@PathVariable Integer id,@PathVariable Integer age){
        return "更新资源,执行put请求方式:id="+id+" aeg="+age;
    }

    /*删除资源
    * */
    @DeleteMapping("/student/{id}")
    public String removeStudent(@PathVariable Integer id){
        return "删除资源,执行delete请求方式:id="+id;
    }

    @PutMapping("/student/test")
    //@RequestMapping(value = "/student/test",method = RequestMethod.GET)
    public String test(){
        return "执行student/test,使用的请求方式post";
    }
}

配置文件:application.properties

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

testrest.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h3>测试rest请求的方式</h3>
  <form action="student/test" method="put">
      <input type="submit" value="注册学生">
  </form>
</body>
</html>

运行主启动类:Application

package com.bjpowernode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

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

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

 application.properties:开启put,delete请求

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

#启用put,delete请求
spring.mvc.hiddenmethod.filter.enabled=true

testrest.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h3>测试rest请求的方式</h3>
  <form action="student/test" method="post">
      <!--使用隐藏域-->
      <input type="hidden" name="_method" value="put">
      <input type="submit" value="put测试请求注方式">
  </form>
  <br>

  <form action="student/testdelete" method="post">
      <!--使用隐藏域-->
      <input type="hidden" name="_method" value="delete">
      <input type="submit" value="delete测试请求注方式">
  </form>
</body>
</html>

在MyRestController:中添加一个delete方法

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

点击put测试按钮:

 

点击delete测试:

 

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

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

 

 

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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐