Learn from:尚硅谷
注解
@PathVariable、
可以获取path中我们选择自定义的地方的数据
@RequestHeader、
可以获取请求头中的数据
@RequestParam、
可以获取url的请求参数
@CookieValue、
用于获取cookie的值
@GetMapping("/anima/{id}/create/{name}")
public Map<String, Object> getAni(@PathVariable String id,
@PathVariable String name,
@PathVariable Map<String, Object> pv,
@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map<String, Object> rh,
@RequestParam("year") String year,
@RequestParam("likes") Integer likes,
@RequestParam Map<String, Object> rp,
@CookieValue("Idea-216775ce") String Idea,
@CookieValue("Idea-216775ce") Cookie cookie){
Map<String, Object> mp = new HashMap<>();
mp.put("anima", id);
mp.put("create", name);
mp.put("pv",pv);
mp.put("user-agent",userAgent);
mp.put("RequestHeader", rh);
mp.put("year",year);
mp.put("likes", likes);
mp.put("RequestParam", rp);
mp.put("idea", Idea);
mp.put("CookieValue", cookie);
return mp;
}
http://localhost:8888/anima/2021/create/化物语?year=2021&likes=22223
下面是返回的数据
@RequestBody
用于获取请求体
@PostMapping("/save")
public Map<String, Object> postMethod(@RequestBody String content){
Map<String, Object> mp = new HashMap<>();
mp.put("content", content);
return mp;
}
访问的表单
<form action="/save" method="post">
用户名<input name="username"><br>
密 码<input name="passwd"><br>
<input type="submit" value="提交">
</form>
提交
返回的是表单的数据的请求参数
@RequestAttribute
获取request域的属性
这里实现了一个转发的功能。
@GetMapping("/goto")
public String goToPage(HttpServletRequest request){
request.setAttribute("msg","成功了。。。");
request.setAttribute("code","200");
return "forward:/success"; //转发到success请求
}
@ResponseBody
@GetMapping("/success")
public Map success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") String code,
HttpServletRequest request){
Map<String, Object> mp = new HashMap<>();
Object msg1 = request.getAttribute("msg");
mp.put("msg1",msg1);
mp.put("msg",msg);
return mp;
}
@MatrixVariable、
用来使用矩阵变量的方法,可以在当cookies禁用的时候使用url拼接jsessionid来获取数据。
需要注意的是该模式一般情况下是被禁用的
需要自己在容器里添加一个组件WebMvcConfigurer,并设置它的urlPathHelper的RemoveSemicolonContent为false。
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer(){
@Override
public void configurePathMatch(PathMatchConfigurer configurer){
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
下面是两种情况
// http://localhost:8888/cars/sell;low=20;brand=audi,byd
@GetMapping("/cars/{path}")
public Map<String,Object> carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brand,
@PathVariable("path") String path){
Map<String,Object> mp = new HashMap<>();
mp.put("low", low);
mp.put("brand",brand);
mp.put("path",path);
return mp;
}
// 不同的path可以使用相同的value值
// http://localhost:8888/boss/2;name=zshh/5;name=koyomi
@GetMapping("/boss/{bossId}/{empId}")
public Map<String,Object> boss(@MatrixVariable(value = "name", pathVar = "bossId") String empName,
@MatrixVariable(value = "name", pathVar = "empId") String bossName){
Map<String,Object> mp = new HashMap<>();
mp.put("empName",empName);
mp.put("bossName",bossName);
return mp;
}
第一种情况,单个属性多个值。
第二种情况,在不同的path中设置请求参数
@ModelAttribute、
被@ModelAttribute注解的方法所在方法的controller内的所有方法在执行前都会被执行。因此对于一个controller映射多个URL的用法来说,要谨慎使用。
@ModelAttribute
public void accessname(@CookieValue("Idea-216775ce") Cookie cookie){
System.out.println(cookie.getName());
}
在该方法存在的Controller内的所有请求访问前都会输出
Idea-216775ce