Six usage summary of @questMappering

Summarize the usage of @RequestMapping in spring mvc. 

1) The most basic, applied at the method level, for example: 
   
Java code   Favorite code
  1. @RequestMapping(value="/departments")  
  2. public String simplePattern(){  
  3.   
  4.   System.out.println("simplePattern method was called");  
  5.   return "someResult";  
  6.   
  7. }  

   When visiting http://localhost/xxxx/departments, the simplePattern method will be called 

2) Parameter binding 
  
Java code   Favorite code
  1. @RequestMapping(value="/departments")  
  2. public String findDepatment(  
  3.   @RequestParam("departmentId") String departmentId){  
  4.     
  5.     System.out.println("Find department with ID: " + departmentId);  
  6.     return "someResult";  
  7.   
  8. }  

   
  An access form like this: 

   /departments?departmentId=23 can trigger access to the findDepatment method. 

3 REST style parameters 
  
Java code   Favorite code
  1. @RequestMapping(value="/departments/{departmentId}")  
  2. public  String findDepatment ( athPathVariable  String departmentId)  
  3.   
  4.   System.out.println("Find department with ID: " + departmentId);  
  5.   return "someResult";  
  6.   
  7. }  

  
  REST-style address access, such as: 
/departments/23, where (@PathVariable is used to receive rest-style parameters 

4 REST-style parameter binding form 2) Let's 
   look at the example first, which is a bit like the previous one: 
Java code   Favorite code
  1. @RequestMapping(value="/departments/{departmentId}")  
  2. public String findDepatmentAlternative(  
  3.   @PathVariable("departmentId") String someDepartmentId){  
  4.   
  5.     System.out.println("Find department with ID: " + someDepartmentId);  
  6.     return "someResult";  
  7.   
  8. }  


   这个有点不同,就是接收形如/departments/23的URL访问,把23作为传入的departmetnId,,但是在实际的方法findDepatmentAlternative中,使用 
@PathVariable("departmentId") String someDepartmentId,将其绑定为 
someDepartmentId,所以这里someDepartmentId为23 

5 url中同时绑定多个id 
 
Java代码   Favorite code
  1. @RequestMapping(value="/departments/{departmentId}/employees/{employeeId}")  
  2. public String findEmployee(  
  3.   @PathVariable String departmentId,  
  4.   @PathVariable String employeeId){  
  5.   
  6.     System.out.println("Find employee with ID: " + employeeId +   
  7.       " from department: " + departmentId);  
  8.     return "someResult";  
  9.   
  10. }  


   这个其实也比较好理解了。 

6 支持正则表达式 
  
Java代码   Favorite code
  1. @RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}")  
  2. public String regularExpression(  
  3.   @PathVariable String textualPart,  
  4.   @PathVariable String numericPart){  
  5.   
  6.     System.out.println("Textual part: " + textualPart +   
  7.       ", numeric part: " + numericPart);  
  8.     return "someResult";  
  9. }  


   For example, the following URL: /sometext.123 will output: 
Textual part: sometext, numeric part: 123. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325663333&siteId=291194637