常用参数注解

路径变量@PathVariable

①获取指定路径变量:

 @GetMapping("/car/{id}/owner/{userName}")
    public Map<String,Object> getCar(@PathVariable("id") int id,
                         @PathVariable("userName") String userName){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("userName",userName);

        return map;
    }

 请求:http://localhost:8080/car/1/owner/zhangsan

返回:{"id":"1","userName":"zhangsan"}

②获取所有路径变量:

//使用map提取所有的路径变量,map必须是String类型
    @GetMapping("/car/{id}/owner/{userName}/{age}")
    public Map<String,String> getCar(@PathVariable() Map<String,String> kv){

        return kv;
    }

请求:http://localhost:8080/car/2/owner/lisi/30

返回:{"id":"2","userName":"lisi","age":"30"}

 

获取请求头@RequestHeader

①获取指定请求头

@GetMapping("/car/{id}/owner/{userName}")
    public Map<String,Object> getCar(@PathVariable("id") int id,
                         @PathVariable("userName") String userName,
                                    @RequestHeader("User-Agent") String userAgent){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("userName",userName);
        map.put("userAgent",userAgent);
        return map;
    } 

 

 请求返回:

{"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36","id":1,"userName":"zhangsan"}

 ②获取所有的请求头

@GetMapping("/car/{id}/owner/{userName}")
public Map<String,String> getCar(@PathVariable("id") int id,
                     @PathVariable("userName") String userName,
                                @RequestHeader Map<String,String> heads){

    return heads;
}
{"host":"localhost:8080","connection":"keep-alive","cache-control":"max-age=0","sec-ch-ua":"\"Google Chrome\";v=\"105\", \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"105\"","sec-ch-ua-mobile":"?0","sec-ch-ua-platform":"\"Windows\"","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1","sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br","accept-language":"zh-CN,zh;q=0.9","cookie":"Idea-71259e56=b05a3fec-a744-4e6b-aeb2-f38a5c6b6dd0; Webstorm-142c3cad=9dee10a5-f1c0-4ed5-97c6-4b442beeb5b3"}

 

获取请求参数@RequestParam

 ①获取指定的参数

@GetMapping("/userInfo")
    public Map<String,Object> getUser(@RequestParam("name") String name,
                                      @RequestParam("hobbies") List<String> hobbies){

        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("hobbies",hobbies);
        return map;
    }

 

 

获取cookie的值@CookieValue

①方式一

@GetMapping("/userInfo")
    public Map<String,Object> getUser(@CookieValue("Webstorm-142c3cad") String p){

        Map<String,Object> map = new HashMap<>();
        map.put("Webstorm",p);
        return map;
    }

 

 

 请求:http://localhost:8080/userInfo?name=xiaoming

返回:{"Webstorm":"9dee10a5-f1c0-4ed5-97c6-4b442beeb5b3"}

②方式二:声明为Cookie类型

 @GetMapping("/userInfo")
    public String getUser(@CookieValue("Idea-71259e56") Cookie cookie){
        System.out.println(cookie.getName()+":"+cookie.getValue());

        return "1";
    }

 

获取请求体@RequestBody(post请求)  

public Map<String,Object> getUser(@RequestBody String content){
     Map<String,Object> map = new HashMap<>();
     map.put("content",content);

     return map;
}

 

 

 


 

返回字符串类型

    @PostMapping("/userInfo")
    public String getUser(@RequestBody String content){
        return content;
    }

 

 

 

  

  

  

 

 

 

 

  

posted @ 2022-09-25 14:02  Mr_sven  阅读(112)  评论(0编辑  收藏  举报