springboot通过GET、POST、PUT、DELETE来实现数据的增删改查

GET:一般用于查询数据,采用明文传输

POST:一般用于插入数据

PUT:一般用于更新数据

DELETE:一般用于删除数据

 

@PathVariable:获取url中的数据

    @GetMapping(value = "/get/{id}")
    public String getMethodId(@PathVariable(value = "id") int id)
    {
        return "@PathVariable注解:getMethodId()" + id;
    }
// @PathVariable注解:getMethodId()12345

 GetMapping几种获取数据的方式

@GetMapping(value = "/test/get")
    public String testGetBody(Student student)
    {
        return student.toString();
        //GET http://localhost:8001/test/get?name=hello&age=12
        //
        //HTTP/1.1 200 
        //Student{name='hello', age=12}
    }

PostMapping获取参数

@PostMapping(value = "/test/post")
public String testPostBody(Student student)
{
return student.toString();
//POST http://localhost:8001/test/post?name=post&age=13
//
//HTTP/1.1 200
//
//Student{name='post', age=13}

//POST http://localhost:8001/test/post
//Content-Type: application/json
//
//{
//"name":"post",
//"age":12
//}

// HTTP/1.1 200
// Student{name='null', age=null}

   //如果使用application/x-www-form-urlencoded代替application/json也可以

}

@PostMapping(value = "/test/request")
public String testRequestBodyBody(@RequestBody Student student)
{
return student.toString();
//POST http://localhost:8001/test/request
//Content-Type: application/json
//
//{
// "name":"post",
// "age":12
//}

// HTTP/1.1 200
// Student{name='RequestBody', age=12}
}

 

@RequestParam注解接收参数

语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)

value:参数名

required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错。

defaultValue:默认参数值,如果设置了该值,required=true将失效,自动为false,如果没有传该参数,就使用默认值