Spring mvc@RequestMapping Rest风格传参

  • REST风格的参数
@RequestMapping(value="/list{name}")  
public void findName(@PathVariable String name){  
  System.out.println("name: " + name);  
}
@RequestMapping(value="/list{name}")  
public void findNameAlternative(@PathVariable("name") String someName){  
    System.out.println("name: " + someName);  
}
  • url中同时绑定多个参数
@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")  
    public void findEmployee(  
      @PathVariable String departmentId,  
      @PathVariable String employeeId){  
      
        System.out.println("Find employee with ID: " + employeeId +   
          " from department: " + departmentId);   
      
    } 

 

  • 正则表达式
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")  
public void regularExpression(  
  @PathVariable String textualPart,  
  @PathVariable String numericPart){  
  
    System.out.println("Textual part: " + textualPart +   
      ", numeric part: " + numericPart);   
}

 

说点什么

avatar