@PathVariable (路径变量)
获取路径上的变量的值
例如发送的请求:/car/1/owner/asd
想获得传来的car的id的值,owner的username的值,就可以使用@PathVariable
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> PathVariablePathVariable(
@PathVariable("id") String id,
@PathVariable("username") String username,
//这里必须得是<String,String>类型
@PathVariable Map<String,String> pv) {
Map<String,Object> map=new HashMap<>();
map.put("id",id);
map.put("username",username);
map.put("pv",pv);
return map;
}
<a href="/car/2/owner/asd">/car/{id}/owner/{username}</a>
代码运行结果:
得到对应路径上传来的值,也可用一个map得到传入的所有参数,不过必须得是Map<String,String>类型
@RequestHeader (获取请求头)
可以得到请求头里的内容
// /car/1/owener/asd
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> PathVariablePathVariable(@RequestHeader("Host") String myhost,
@RequestHeader Map<String,String> header) {
Map<String,Object> map=new HashMap<>();
map.put("host",myhost);
map.put("header",header);
return map;
}
同样也可用一个map得到传入的所有参数,是Map<String,String>类型
@RequestParam(获取请求参数)
得到路径上请求的参数
@GetMapping("/param1")
public String param1(@RequestParam("name")String name){
return name;
}
@GetMapping("/param2")
public String param2(@RequestParam(value = "name",required = false)String name){
return name;
}
@GetMapping("/param3")
public String param3(@RequestParam(value = "name",required = false,defaultValue = "qwe")String name){
return name;
}
@GetMapping("/param4")
public String param4(@RequestParam(value = "name",required = true,defaultValue = "qwe")String name){
return name;
}
http://localhost:8080/param1?name=789
获得路径上通过?传来的参数
有两个属性
required=true(默认) | true,必须传递这个参数,否则报错 false,则不会报错 |
defaultValue="xxx" | 给这个参数设置了默认值,这样设置以后,上面required这个属性true或者false都可以 |
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> PathVariablePathVariable(
@RequestParam(value = "age",required = false) Integer age,
@RequestParam("sports") List<String> sports,
//想得到路径中传来的多个值,单独接收用List,所有参数一起接收用MultiValueMap
@RequestParam MultiValueMap<String, String> params
) {
Map<String,Object> map=new HashMap<>();
map.put("age",age);
map.put("sports",sports);
map.put("params",params);
return map;
}
<a href="/car?&sports=basketball&sports=football">222</a>
这里sports就传了两个值过来,不能用Map<String,String>接收,得用MultiValueMap<String, String>接收。
@CookieValue(获取cookie)
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> PathVariablePathVariable(
@CookieValue("Idea-e6ddc5c9") String cookies,
@CookieValue("Idea-e6ddc5c9") Cookie cookie
) {
Map<String,Object> map=new HashMap<>();
map.put("cookies",cookies);
map.put("123",cookie);
return map;
}
得到请求里的cookie,可以单独用String得到,也可以封装在cookie对象里
@RequestBody(获取请求体,POST请求)
用于post请求中获得post的请求体
@PostMapping("/save")
public Map save(@RequestBody String contents){
Map<String,Object> map=new HashMap<>();
map.put("ccc",contents);
return map;
}
返回的是post 请求中的请求体,用于后端接收前端传来的json格式的数据。也可以用对象得到传来的值,对象的属性和类型
@RequestAttribute(获取request域属性)
获取HTTP的请求(request)对象属性值,用来传递给控制器的参数。
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute("code") Integer code,
@RequestAttribute("msg") String msg,HttpServletRequest request
){
Object msg1 = request.getAttribute("msg");
Object code2 = request.getAttribute("code");
Map<String,Object> map=new HashMap<>();
map.put("msg",msg);
map.put("code",code);
map.put("msg1",msg1);
map.put("code1",code2);
return map;
}
得到request域中“msg”,"code"中的值
@MatrixVariable(矩阵变量)
默认是被禁用了的,需要自己配置开启。
@Configuration(proxyBeanMethods = false)
public class Myconfig {
/**
* 不移除;后面的内容 矩阵变量就可以生效
* @return
*/
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper=new UrlPathHelper();
//这里设置urlPathHelper的RemoveSemicolonContent属性为false
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
}
/*
1. 获取单个路径片段中的参数
请求URI为 /Demo2/66;color=red;year=2020
*/
@GetMapping("/demo1/{id}")
public String test1(@PathVariable String id, @MatrixVariable String color, @MatrixVariable String year){
return id+color+year;
}
/*
2. 获取单个路径片段中的参数
请求URI为 /Demo2/color=red;year=2020
*/
@GetMapping("/demo1/{id}")
public String test2(@MatrixVariable String color, @MatrixVariable String year){
return color+year;
}
/*
3. 获取不同路径片段中的参数
请求URI为 /Demo2/66;color=red;year=2020/pets/77;color=blue;year=2019
*/
@GetMapping("/Demo2/{id1}/pets/{id2}")
public String test3(@PathVariable String id1, @PathVariable String id2,
@MatrixVariable(name="color", pathVar="id1") String color1, @MatrixVariable(name="year", pathVar="id1") String year1,
@MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){
return color1+year1+color2+year2;
}
/*
4. 获取不同路径片段中的参数
请求URI为 /Demo2/color=red;year=2020/pets/77;color=blue;year=2019
*/
@GetMapping("/Demo2/{id1}/pets/{id2}")
public String test4(@PathVariable String id2,
@MatrixVariable(name="color", pathVar="id1") String color1, @MatrixVariable(name="year", pathVar="id1") String year1,
@MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){
return id2+color1+year1+color2+year2;
}
/*
5. 通过Map获取所有或指定路径下的所有参数
*/
@GetMapping("/Demo3/{id1}/pets/{id2}")
public Map test5(@MatrixVariable Map<String,Object> all,
@MatrixVariable(pathVar="id2") Map<String,Object> mapId1) {
Map<String,Object> map=new HashMap<>();
map.put("all",all);
map.put("mapid1",mapId1);
return map;
}
在使用矩阵变量时,要搭配路径使用,否则访问不到。
当使用map接收时,如果不指定pathVar的值,默认只接收第一个路径上的矩阵变量。