@RequestAttribute与@MatrixVariable

@RequestAttribute

它只能使用在方法入参上,从request请求参数中获取对应的属性值。

//路径跳转	
@GetMapping("/goto")
public String goToPage(HttpServletRequest request){

    request.setAttribute("msg","成功了...");
    return "forward:/success";  //转发到  /success请求
}
  @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute(value = "msg",required = false) String msg,
                       HttpServletRequest request){
         Map<String,Object> map = new HashMap<>();
        //HttpServletRequest方式获取请求参数
        Object msg1 = request.getAttribute("msg");
        map.put("msg1",msg1);
        //@RequestAttribute注解方式获取请求参数
        map.put("msg",msg);
        return map;
    }

请求参数:http://localhost:8080/goto

结果:{"msg":"成功了...","msg1":"成功了..."}

@MatrixVariable

用于获取路径中参数的属性

假如现在需要设计一个用于“搜索某部门某些员工可选信息中的部分信息”的API,我们分别使用查询字符串和路径name-value方式来设计对比,看看具体效果:

  • 查询字符串方式:

    /api/v1/users/optional-info?dept=321&name=joh*&fields=hometown,birth

问题:其中的dept和name理应属于users路径,而fields则属于optional-info路径,但现在全部都要挤在查询字符串中。

  • 路径name-value方式:

    /api/v1/users/depts=321;name=joh*/optional-fields/fields=hometown,birth

可以看出路径name-value的方式逻辑上更在理些。

使用方法

多个变量可以使用;分隔,例如:

/cars;color=red;year=2012

如果是一个变量的多个值那么可以使用,分隔

color=red,green,blu

或者可以使用重复的变量名:

color=red;color=green;color=blue

属性

  • value :属性pathVar的别名;
  • pathVar :用于指定name-value参数所在的路径片段名称
  • name :用于指定name-value参数的参数名
  • required :是否为必填值,默认为false
  • defaultValue :设置默认值

举例

/*
1. 获取单个路径片段中的参数                                                    
请求URI为 /Demo2/66;color=red;year=2020                                       */                                                                           @RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)                 
public String test1(@PathVariable String id, @MatrixVariable String color, @MatrixVariable String year){}                                               

/*                                                                         
2. 获取单个路径片段中的参数                                                      
请求URI为 /Demo2/color=red;year=2020                                          
*/                                                                          
 @RequestMapping(path="/Demo1/{id}", method=RequestMethod.GET)                
 public String test2(@MatrixVariable String color, @MatrixVariable String year){}                
/*                                                                           
3. 获取不同路径片段中的参数                                                    
请求URI为 /Demo2/66;color=red;year=2020/pets/77;color=blue;year=2019             */   
@RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET)     
public String test3(@PathVariable String id1, 
                    @PathVariable String id2,        
                    @MatrixVariable(name="color", pathVar="id1") String color1, 
                    @MatrixVariable(name="year", pathVar="id1") String year1,                                    
                    @MatrixVariable(name="color", pathVar="id2") String color2, @MatrixVariable(name="year", pathVar="id2") String year2){}       

/*                                                                          
4. 获取不同路径片段中的参数                                                      
请求URI为 /Demo2/color=red;year=2020/pets/77;color=blue;year=2019            
*/                                                                         
@RequestMapping(path="/Demo2/{id1}/pets/{id2}", method=RequestMethod.GET)    
 public String test4(@PathVariable String id2,                                 
                     @MatrixVariable(name="color", pathVar="id1") String color1, 
                     @MatrixVariable(name="year", pathVar="id1") String year1,                     
                     @MatrixVariable(name="color", pathVar="id2") String color2, 
                     @MatrixVariable(name="year", pathVar="id2") String year2){}     

/*                                                                        
5. 通过Map获取所有或指定路径下的所有参数                                         
*/                                                                           
@RequestMapping(path="/Demo3/{id1}/pets/{id2}", method=RequestMethod.GET)     
public String test5(@MatrixVariable Map<String, Object> all, 
                    @MatrixVariable(pathVar="id1") Map<String, Object> mapId1) {}    
posted @ 2021-03-14 23:02  至安  阅读(481)  评论(0编辑  收藏  举报