注解
@GetMapping("/test/{id}/user/{userName}")
public void testPathVariable(@PathVariable("id") Integer id,
@PathVariable("userName") String userName,
@PathVariable Map<String,String> allPathVariable){
return;
}
@GetMapping("/test")
public void testHeader(@RequestHeader("User-Agent") String userAgent,
@RequestHeader Map<String,String> allHeader){
return;
}
- @RequestParam :获取请求头参数中的参数提供给指定参数使用
public String login(@RequestParam String userName,
@RequestParam("psw") String password,
){
- @CookieValue :获取Cookie的值
- @RequestAttribute :获取request域属性
@GetMapping("/goto")
public String gotoPage(HttpServletRequest httpServletRequest){
httpServletRequest.setAttribute("msg","这是我给success的消息");
httpServletRequest.setAttribute("code",200);
return "forward:/user/success";
}
@ResponseBody
@GetMapping("/success")
public void successPage(@RequestAttribute("msg") String msgFromGoto){
System.out.println("我从GOTO转发到这儿啦,他给我说:"+msgFromGoto);
}
- @RequestBody :获取请求体
- @MatrixVariable:矩阵变量,矩阵变量应该绑定在路径变量中;
例如当cookie被禁用时,使用url重写:/abc;jsessionid=xxxx;把cookie的值使用矩阵变量的方式进行传递;例如: /cars/1;age=20;/2 分号;前面为访问路径,后边为矩阵变量,多个矩阵变量用分号分割
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
@GetMapping("/matrix/{path1}/{path2}")
public Map matrixVarTest(@MatrixVariable(pathVar = "path1") Integer unitId,@MatrixVariable String name,@MatrixVariable(value = "unitId",pathVar ="path2") Integer unitId2 ){
Map<String,Object> map = new HashMap<>();
map.put("unidId",unitId);
map.put("name",name);
map.put("unitId2",unitId2);
return map;
}