@PathVariable
<a href="/Card/1/小黄">@PathVariable(路径变量)</a>
@GetMapping("/Card/{id}/{username}")
public Map<String ,Object> getCard(@PathVariable("id") Integer id,
@PathVariable("username") String name,
@PathVariable Map<String, String> pv){
Map<String,Object> map = new HashMap<>();
map.put("id",id);
map.put("name",name);
map.put("pv",pv);
return map;
}
结果:
@RequestHeader
<a href="/getHeader">@RequestHeader(获取请求头)</a>
@GetMapping("/getHeader")
public Map<String,Object> getHeader(@RequestHeader("Pragma") String Pragma,
@RequestHeader Map<String,String> allHeader){
Map<String,Object> map = new HashMap<>();
map.put("Pragma",Pragma);
map.put("allHeader",allHeader);
return map;
}
结果:
@RequestParam
<a href="/getParam?age=1&interest='吃饭'&interest='睡觉'">@RequestParam(获取请求参数)</a>
@GetMapping("/getParam")
public Map<String,Object> getParam(@RequestParam("age")Integer age,
@RequestParam("interest") List<String> interest){
Map<String,Object> map = new HashMap<>();
map.put("age",age);
map.put("interest",interest);
return map;
}
结果:
@RequestBody
<form method="post" action="/getPerson" >
名字: <input type="text" name="name">
年龄:<input type="text" name="age">
<input type="submit">
</form>
@PostMapping(value = "/getPerson")
public Map<String,Object> getPerson(@RequestBody String Person){
Map<String,Object> map = new HashMap<>();
map.put("Person",Person);
return map;
}
结果:
@RequestAttribute
比如:request.setAttribute(“haha”);请求转发到另外一个映射中。
我们就可以通过@RequestAttribute(“haha”)获取到haha保存的值
等同于request.getAttribute(“haha”);
可以获取到 请求域中的值~
@MatrixVariable
矩阵变量必须有url路径变量才能被解析~
springboot默认关闭了矩阵变量功能(可以通过以下两种方法开启)
//方法一
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
//方法二
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
例一:(只有一个路径变量~)
<a href="/person/id;name=xiao;interest=吃饭,睡觉,打豆豆">@MatrixVariable(矩阵变量)</a>
@RequestMapping("/person/{path}")
public Map<String,Object> getCars(@MatrixVariable("name") String name,
@MatrixVariable("interest") List<String> interest,
@PathVariable("path") String path){
Map<String,Object> map = new HashMap<>();
map.put("name",name);
map.put("interest",interest);
map.put("path",path);
return map;
}
结果:![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6fe5ead6af7c4ad266919df957f6a4e5.png)
例二:(当有两个路径变量的时候!)
<a href="/person/one;age=20/two;age=10">@MatrixVariable(矩阵变量)/person/{path1}/{path2}</a>
@RequestMapping("/person/{path1}/{path2}")
public Map<String,Object> getCars(@MatrixVariable(value = "age",pathVar = "path1") Integer path1 ,
@MatrixVariable(value = "age",pathVar = "path2") Integer path2,
@PathVariable("path1") String pathname1,
@PathVariable("path2") String pathname2){
Map<String,Object> map = new HashMap<>();
map.put("path1", path1);
map.put("path2", path2);
map.put("pathname1",pathname1);
map.put("pathname2",pathname2);
return map;
}