Summary of @ RequestMapping value usage in spring mvc

Summary of six basic usage of @ requestmapping in Spring mvc

Summarize the usage of @ RequestMapping in spring mvc.  

1) Most basic, method level applications, such as:
   
  1. @RequestMapping(value="/departments")  
  2. public String simplePattern(){  
  3.   
  4.   System.out.println("simplePattern method was called");  
  5.   return "someResult";  
  6.   
  7. }  

When you visit http://localhost/xxxx/departments, the simpleplattern method will be called

2) Parameter binding
  
  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. }  

   
The form of access is as follows:

/ departments?departmentId=23 to trigger access to finddepartment method

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

  
Address access in the form of REST, 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. This is a bit like the previous:
  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. }  


This is a little different. It receives URL access like / departments/23, and takes 23 as the incoming departmentnetid. However, in the actual method finddepartmentalternative, it uses
@PathVariable("departmentId") String someDepartmentId, bind it as
someDepartmentId, so here someDepartmentId is 23

5 multiple IDs are bound in the URL at the same time
 
  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. }  


In fact, it's easier to understand.  

6 regular expression support
  
  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. }  

Tags: REST Spring

Posted on Mon, 18 May 2020 08:16:30 -0700 by Elle0000