- @PathVariable
- @RequestHeader
- @RequestParam
- @cookievalue
- @RequestBody
- @RequestAttribute
本篇内容与 SpringMVC获取请求参数、SpringMVC获取请求体、域对象数据共享、SpringMVC的视图这几篇文章内容有重复,就当是复习总结了。
首先,新建Spring项目:demo4,并添加依赖Lombok、Spring Configuration Processer和Spring Web。
新建控制器com.example.boot.controller.Demo4Controller,内容如下。
package com.example.boot.controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class Demo4Controller {
@GetMapping("/car/{id}")
public Map getCar(@PathVariable("id") Integer id){
Map map = new HashMap<>();
map.put("id",id);
return map;
}
@GetMapping("/car/{id}/owner/{username}")
public Map getCar2(@PathVariable("id") Integer id,
@PathVariable("username") String username,
@PathVariable Map pv){
Map map = new HashMap<>();
map.put("id",id);
map.put("username",username);
map.put("pv",pv);
return map;
}
@GetMapping("/car")
public Map getCar3(@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map headers){
Map map = new HashMap<>();
map.put("userAgent",userAgent);
map.put("headers",headers);
return map;
}
@GetMapping("/user/{id}")
public Map getUser(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age,
@RequestParam("hobby") List hobby,
@RequestParam Map params){
Map map = new HashMap<>();
map.put("id",id);
map.put("name",name);
map.put("age",age);
map.put("hobby",hobby);
map.put("params",params);
return map;
}
@GetMapping("/user")
public String getUser2(HttpServletRequest request){
HttpSession httpSession = request.getSession();
return "create cookie";
}
@GetMapping("/user/owner")
public Map getUser3(@cookievalue("JSESSIONID") String sessionID,
@cookievalue("JSESSIONID") cookie cookie){
Map map = new HashMap<>();
map.put("sessionID",sessionID);
map.put("cookie",cookie);
System.out.println(cookie.getName()+"==>"+cookie.getValue());
return map;
}
@PostMapping("/save")
public Map save(@RequestBody String content){
Map map = new HashMap<>();
map.put("content",content);
return map;
}
}
resources.static下新建页面index.html,内容如下。
首页
测试@PathVariable#getCar()
测试@PathVariable#getCar2()
测试@RequestHeader#getCar3()
测试@RequestParam#getUser()
生成cookie数据测试@cookievalue#getUser3()
以上内容主要对以下几个常用的参数注解进行了应用分析。
- @PathVariable
- @RequestHeader
- @RequestParam
- @cookievalue
- @RequestBody
- @RequestAttribute
把要传输给服务器的数据放到请求路径的占位符上,如/uset/{id},在REST风格中很常见。而使用注解@PathVariable则可将占位符上的参数取出并赋值给控制器方法形参。
- 取出占位符上的id。
测试@PathVariable#getCar()
@GetMapping("/car/{id}")
public Map getCar(@PathVariable("id") Integer id){
Map map = new HashMap<>();
map.put("id",id);
return map;
}
- 取出占位符上的id和username;取出占位符上的所有参数,以Map
形式保存。
测试@PathVariable#getCar2()
@GetMapping("/car/{id}/owner/{username}")
public Map getCar2(@PathVariable("id") Integer id,
@PathVariable("username") String username,
@PathVariable Map pv){
Map map = new HashMap<>();
map.put("id",id);
map.put("username",username);
map.put("pv",pv);
return map;
}
@RequestHeader
使用注解@RequestHeader获取请求头信息。
@RequestHeader("User-Agent") String userAgent,获取请求头User-Agent,并赋值给控制器方法getCar3的形参userAgent。
@RequestHeader Map
测试@RequestHeader#getCar3()
@GetMapping("/car")
public Map getCar3(@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map headers){
Map map = new HashMap<>();
map.put("userAgent",userAgent);
map.put("headers",headers);
return map;
}
@RequestParam
使用注解@RequestParam获取请求参数(或查询字符串,就是?后面的参数)。
@RequestParam("name") String name,获取请求参数name,并赋值给控制器方法getUser的形参name。
@RequestParam("age") Integer age,获取请求参数age,并赋值给控制器方法getUser的形参age。
@RequestParam("hobby") List
@RequestParam Map
测试@RequestParam#getUser()
@GetMapping("/user/{id}")
public Map getUser(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age,
@RequestParam("hobby") List hobby,
@RequestParam Map params){
Map map = new HashMap<>();
map.put("id",id);
map.put("name",name);
map.put("age",age);
map.put("hobby",hobby);
map.put("params",params);
return map;
}
@cookievalue
使用注解@cookievalue获取cookie数据。
@cookievalue("JSESSIONID") String sessionID,从请求中获取cookie数据,并赋值给控制器方法getUser3形参:字符串类型sessionID。
@cookievalue("JSESSIONID") cookie cookie,从请求中获取cookie数据,并赋值给控制器方法getUser3的形参:cookie对象cookie。
生成cookie数据测试@cookievalue#getUser3()
@GetMapping("/user")
public String getUser2(HttpServletRequest request){
HttpSession httpSession = request.getSession();
return "create cookie";
}
@GetMapping("/user/owner")
public Map getUser3(@cookievalue("JSESSIONID") String sessionID,
@cookievalue("JSESSIONID") cookie cookie){
Map map = new HashMap<>();
map.put("sessionID",sessionID);
map.put("cookie",cookie);
System.out.println(cookie.getName()+"==>"+cookie.getValue());
return map;
}
首次点击链接"生成cookie数据"访问接口/user时,调用request.getSession()会得到一个session对象,session对象的id会以键值对的形式保存在cookie中(JSESSIonID=随机数),且cookie会被回写至响应头中。
再次访问接口/user时,请求的请求头会默认携带上述cookie,但请求的响应头中不在有该cookie了。
使用注解@RequestBody获取请求的请求体。
@PostMapping("/save")
public Map save(@RequestBody String content){
Map map = new HashMap<>();
map.put("content",content);
return map;
}
@RequestAttribute
使用注解@RequestAttribute获取request域属性。
Java中常说的域有:request域、session域和应用域,关于如何在域中共享对象数据,可以参考域对象数据共享这篇文章。
新建一个控制器com.example.boot.controller.HelloController(注意哈,使用的是@Controller,不是@RestController)。
package com.example.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
public class HelloController {
@GetMapping("/test")
public String test(HttpServletRequest request){
request.setAttribute("msg","请求成功");
request.setAttribute("code",200);
return "forward:/success";
}
@GetMapping("/success")
@ResponseBody
public Map success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") Integer code,
HttpServletRequest request){
Map map = new HashMap<>();
Object msg1 = request.getAttribute("msg");
Object code1 = request.getAttribute("code");
map.put("msg",msg);
map.put("code",code);
map.put("msg1",msg1);
map.put("code1",code1);
return map;
}
}
index.html中添加如下链接。
测试@RequestAttribute#test()
本例中,发送请求访问接口/test时,forward:/success会将请求转发至/success。关于视图(转发或重定向)可以访问SpringMVC的视图这篇文章。



