注解
- @PathVariable : 路径变量,如果
@GetMapping("/test/{id}/user/{userName}")
public void testPathVariable(@PathVariable("id") Integer id,
@PathVariable("userName") String userName,
//通过Map可以将所有路径参数存入(注:必须用String,String装载)
@PathVariable Map<String,String> allPathVariable){
return;
}
- @RequestHeader : 获取请求头
@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); //将移除;后边的内容,设置为false,默认值为true;
configurer.setUrlPathHelper(urlPathHelper);
}
//springboot默认禁用了矩阵变量功能,需要手动开启
//对于路径的处理,通过WebMvcConfigurer中的UrlPathHelper进行解析,其中removeSemicolonContent设置是否移除分号后的内容,默认为true
//http://localhost:9090/user/matrix/unitId=112;name=123
//必须有请求路径时,才能使用矩阵变量
@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;
}
版权声明:本文为sinat_33886644原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。