注解1@PathVariable
获取路径变量
使用的形式:/car/{id}/owner/{username}
例如:
@GetMapping("/car/{id}/owner/{username}")
public Map<String,Object> get(@PathVariable("id") Integer id,
@PathVariable("username") String name,
@PathVariable Map<String,Object> map){
Map<String,Object> map1= new HashMap<>();
map1.put("id",id);
map1.put("username",name);
map1.put("map",map);
return map1;
}
注解2,@RequestHeader
获取请求头
@GetMapping("/car")
public Map<String,Object> get(@RequestHeader("User-Agent") String header,
@RequestHeader Map<String,Object> map
){
Map<String,Object> map1= new HashMap<>();
map1.put("User-Agent",header);
map1.put("map",map);
return map1;
}
注解3,@RequestParam
获取请求参数
@GetMapping("/car")
public Map<String,Object> get(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam Map<String,Object> map
){
Map<String,Object> map1= new HashMap<>();
map1.put("id",id);
map1.put("name",name);
map1.put("map",map);
return map1;
}
注解4,@CookieValue获取cookie的值
@GetMapping("/car")
public Map<String,Object> get(@CookieValue("Idea-8fe00652") String Idea,
@CookieValue("Idea-8fe00652") Cookie cookie
){
Map<String,Object> map1= new HashMap<>();
map1.put("Idea-8fe00652",Idea);
map1.put("cookie",cookie);
return map1;
}
注解5,@RequestBody
获取请求体的值
<form action="/save" method="post">
姓名:<input name="姓名" >
年龄:<input aria="年龄" >
<input value="button" type="submit">
</form>
@PostMapping("/save")
public Map postMethod(@RequestBody String content){
Map<String,Object> map = new HashMap<>();
map.put("content",content);
return map;
}
注解6,@RequestAttribute
获取request的域属性
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
public class requestController {
@GetMapping("/goto")
public String Goto(HttpServletRequest request){
request.setAttribute("msg","hah");
request.setAttribute("mn","xsa");
return "forward:/sucess";
}
@ResponseBody
@GetMapping("/sucess")
public Map<String,Object> sucess(HttpServletRequest request,
@RequestAttribute("msg") String msg,
@RequestAttribute("mn") String mn){
Map<String, Object> map=new HashMap<>();
Object msg1 = request.getAttribute("msg");
map.put("mag",msg1);
map.put("mn",mn);
return map;
}
}
注解7,@MatrixVariable
矩阵变量
矩阵变量默认时关闭的,因此首先是要开启矩阵变量
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
开启后进行测试
//1、语法: 请求路径:/cars/sell;low=34;brand=byd,audi,yd
//2、SpringBoot默认是禁用了矩阵变量的功能
// 手动开启:原理。对于路径的处理。UrlPathHelper进行解析。
// removeSemicolonContent(移除分号内容)支持矩阵变量的
//3、矩阵变量必须有url路径变量才能被解析
@GetMapping("/cars/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brand,
@PathVariable("path") String path){
Map<String,Object> map = new HashMap<>();
map.put("low",low);
map.put("brand",brand);
map.put("path",path);
return map;
}
// /boss/1;age=20/2;age=10
@GetMapping("/boss/{bossId}/{empId}")
public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
@MatrixVariable(value = "age",pathVar = "empId") Integer empAge) {
Map<String, Object> map = new HashMap<>();
map.put("bossAge", bossAge);
map.put("empAge", empAge);
return map;
}