提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
获取路径变量,可以获取路径中包含该名称的变量值
示例如下:
@RequestMapping("/car/{id}/owner/{username}")
public Map getCar(@PathVariable("id") Integer id,
@PathVariable("username") String name,
@PathVariable Map pv){
map.put("id",id);
map.put("username",name);
map.put("pv",pv);
}
在地址栏输入localhost:8080/car/1/owner/zhangsan得到如下运行结果
获取请求头信息,如果注解有值则获取对应值的名称的请求头信息,若没有则获取所有的请求头信息
示例如下
@RestController
public class ParamController {
@RequestMapping("/car/{id}/owner/{username}")
public Map getCar(@PathVariable("id") Integer id,
@PathVariable("username") String name,
@PathVariable Map pv,
@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map header
) {
Map map = new HashMap<>();
map.put("id",id);
map.put("username",name);
map.put("pv",pv);
map.put("userAgent",userAgent);
map.put("header",header);
return map;
}
获取参数值,即在地址栏后添加http: //localhost:8888/car/3/owner/lis?age=18&inters=basketball
即可获取对应的值,示例如下
@RestController
public class ParamController {
@RequestMapping("/car/{id}/owner/{username}")
public Map getCar(@PathVariable("id") Integer id,
@PathVariable("username") String name,
@PathVariable Map pv,
@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map header,
@RequestParam("age") Integer age,
@RequestParam("inters") List inters,
@RequestParam Map params
) {
Map map = new HashMap<>();
map.put("age",age);
map.put("inters",inters);
return map;
}
获取对应名称的value值,示例如下
@RestController
public class ParamController {
@RequestMapping("/car/{id}/owner/{username}")
public Map getCar(@CookieValue("Idea-8f9a4da6") String idea,
@CookieValue("Idea-8f9a4da6") Cookie cookie
) {
Map map = new HashMap<>();
map.put("idea",idea);
map.put("cookie",cookie);
return map;
}
获取请求体【post】,即提交表单中的信息
@PostMapping("/save")
public Map postMethod(@RequestBody String content) {
Map map = new HashMap<>();
map.put("content",content);
return map;
}
获取request域中设置的属性
示例如下:request.getAttribute()也可以获取属性
@Controller
public class RequestController {
@GetMapping("/goto")
public String go(HttpServletRequest request) {
request.setAttribute("msg","成功了");
request.setAttribute("code",200);
return "forward:/success";
}
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") Integer code,
HttpServletRequest request) {
Object msg1 = request.getAttribute("msg");
Map map = new HashMap<>();
map.put("msg1",msg1);
map.put("msg",msg);
return map;
}
}
获取矩阵变量,应该绑定在路径变量中,进行路径重写,使用矩阵变量的方式可以传递cookie的值。
使用;,分隔路径和矩阵变量的值。springboot默认禁用了矩阵变量的功能,需要手动开启。
原理:整个路径的处理都是URLPathHeader处理的,removeSemicolonContent移除分号内容。即截去分号之后的内容。
开启方式:@Bean添加WebConfigurer
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
//不移除分号之后的内容
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
继承webMvcConfigurer接口,然后重写configurePathMatch()方法
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
//不移除分号之后的内容
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
controller层
@ResponseBody
@GetMapping("/cars/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List brand,
@PathVariable("path") String path){
Map map = new HashMap<>();
map.put("low",low);
map.put("brand",brand);
map.put("path",path);
return map;
}
请求方式是
矩阵变量
当有多个相同名字的请求参数时,例如/cars/1;age=20/2;age=10,在controller层使用@MatrixVariable(value= “age”,pathVar = “boosId”);来区分参数值,否则只能取到第一个值
@ResponseBody
@GetMapping("/cars/{carId}/{lookid}")
public Map carsId(@MatrixVariable(value = "id",pathVar = "carId") Integer carId,
@MatrixVariable(value = "id",pathVar = "lookid") Integer lookId){
Map map = new HashMap<>();
map.put("carId",carId);
map.put("lookid",lookId);
return map;
}
矩阵变量