@RequestParam,@PathVariable和@RequestBody三者区别

@RequestParam:获取请求头上的参数:

例:

http://localhost:8090/hello?id=2
//使用@RequestParam注解获取id
public String Demo1(@RequestParam String id){
    System.out.println("链接中请求参数的id:"+id);
    return null;
}
@RequestBody:获取请求体中的参数,可以将json数据映射到Map或实体类上
//异步请求部分代码
        $.ajax({
     url:"/hello",
     type:"POST",
     data:'{"id":"123","name":"chenyc"}',
     content-type:"application/json charset=utf-8",
     success:function(data){
       alert(data);
     }
  });
//@requestBody注解获取数据代码
   @RequestMapping("/hello")
  public String hello(@requestBody User user){
  System.out.println("id:"+user.getId()+";"+"name:"+user.getName());
   }
@PathVariable注解

顾名思义:路径变量,即是获取链接路径上的变量,例如获取下面链接的id:

//链接(注意比较上面一条链接)
http://localhost:8090/hello/2
//使用@PathVariable注解获取id
@RequestMapping(value = "/getBook/{id}", method = RequestMethod.GET)
public String getBook(@PathVariable Integer id) {
     try {
            system.out.println("路径上的id:"+id);
        } catch (ParseException e) {
            e.printStackTrace();
    }
    return null;
}

参考:https://www.cnblogs.com/chengxiaodi/p/11324611.html

你可能感兴趣的:(@RequestParam,@PathVariable和@RequestBody三者区别)