day133-spring boot常用参数注解

发布时间 2023-11-30 21:27:52作者: 北海之上

spring boot常用参数注解

注解:

  • @PathVariable 路径变量

  • @RequestHeader 获取请求头

  • @RequestParam 获取请求参数(指问号后的参数,url?a=1&b=2)

  • @CookieValue 获取Cookie值

  • @RequestAttribute 获取request域属性

  • @RequestBody 获取请求体[POST]

  • @MatrixVariable 矩阵变量

  • @ModelAttribute

案例

@RestController
public class ParameterTestController {


   // car/2/owner/zhangsan
   @GetMapping("/car/{id}/owner/{username}")
   public Map<String,Object> getCar(@PathVariable("id") Integer id,
                                    @PathVariable("username") String name,
                                    @PathVariable Map<String,String> pv,
                                    @RequestHeader("User-Agent") String userAgent,
                                    @RequestHeader Map<String,String> header,
                                    @RequestParam("age") Integer age,
                                    @RequestParam("inters") List<String> inters,
                                    @RequestParam Map<String,String> params,
                                    @CookieValue("_ga") String _ga,
                                    @CookieValue("_ga") Cookie cookie){

       Map<String,Object> map = new HashMap<>();

//       map.put("id",id);
//       map.put("name",name);
//       map.put("pv",pv);
//       map.put("userAgent",userAgent);
//       map.put("headers",header);
       map.put("age",age);
       map.put("inters",inters);
       map.put("params",params);
       map.put("_ga",_ga);
       System.out.println(cookie.getName()+"===>"+cookie.getValue());
       return map;
  }


   @PostMapping("/save")
   public Map postMethod(@RequestBody String content){
       Map<String,Object> map = new HashMap<>();
       map.put("content",content);
       return map;
  }
}