@RequestMapping(value = "/save15")
@ResponseBody
public void save15(@RequestParam(value = "name",required = false,defaultValue = "苏七") String userName)
{
System.out.println(userName);
}
浏览器携带的参数为name,注解到save15方法的参数为userName
代码实现:
@RequestMapping(value = "/save16/{name}",method = RequestMethod.GET)//localhost:8080/user/save16/zhangsan
@ResponseBody
public void save17(@PathVariable(value = "name",required = true) String userName)
{
System.out.println(userName);
}
获得浏览器传递的请求头
@RequestMapping(value = "/save19")
@ResponseBody
public void save19(@RequestHeader(value = "User-Agent",required = true)String user_agent)
{
System.out.println(user_agent);
}
使用@RequestHeader只能获取到请求头的cookie,有时候需要获取cookie中的键值对的值
@RequestMapping(value = "/save20")
@ResponseBody
public void save20(@CookieValue(value = "JSESSIONID",required = true)String cookie)
{
//JSESSIONID代表cookie中的键
System.out.println(cookie);
}