SpringBoot 中常用注解@PathVaribale/@RequestParam的介绍

文章目录

        • @PathVaribale
        • @RequestParam

@PathVaribale

@PathVaribale 获取url中的数据,这种方式也叫路径传参,及将参数当成路径传进来
路径:http://localhost:8080/1
接口配置:

 @GetMapping(value = "{id}")  
 public String getPathParam(@PathVariable("id") int id){
   return id;
}
//@PathVariable 不写的话,报错:"Missing URI template variable 'id' for method parameter of type int",
//@PathVariable("id") 中“id”不写,{id}需要与形参id一致,不然报错

@RequestParam

@RequestParam 获取请求参数的值
路径:http://localhost:8080/test?id=1
接口配置:

 @GetMapping(value = "/test")  
 public String getRequestParam(@RequestParam("id") Integer id){
   return id;
}
//当不输入id后面的值,返回 null
//当不输入?id,报错:Whitelable error page
//当不写 @RequestParam("id") 可传可不传参数

你可能感兴趣的:(java,spring,boot)