Spring MVC @RequestMapping example
Introduction
Spring MVC @RequestMapping annotation used in Controllers allow us not only to implement URL pattern matching but also to extract variables from the request URL itself. We will go through different possible scenarios in the next section to provide a better understanding.
Simple scenario
@RequestMapping(value="/departments") public String simplePattern(){ System.out.println("simplePattern method was called"); return "someResult"; }
Testing URL: /departments
In this simple scenario we are defining that simplePattern() method should be called when the URL matches /departments.
Request parameter binding
@RequestMapping(value="/departments") public String findDepatment( @RequestParam("departmentId") String departmentId){ System.out.println("Find department with ID: " + departmentId); return "someResult"; }
Testing URL: /departments?departmentId=23
In this scenario we are fetching the parameter departmentId from the request URL - departmentId=23 - and mapping it into the String departmentId method parameter. The method will print: Find department with ID: 23
Extracting path variables
@RequestMapping(value="/departments/{departmentId}") public String findDepatment(@PathVariable String departmentId){ System.out.println("Find department with ID: " + departmentId); return "someResult"; }
Testing URL: /departments/23
In this scenario we are matching the method with the pattern /departments/{departmentId}. This means that the section between brackets - {departmentId} - is a variable. Spring automatically matches a path variable to a parameter that has the exact same name, so this method will print: Find department with ID: 23.
Extracting path variables alternative
@RequestMapping(value="/departments/{departmentId}") public String findDepatmentAlternative( @PathVariable("departmentId") String someDepartmentId){ System.out.println("Find department with ID: " + someDepartmentId); return "someResult"; }
Testing URL: /departments/23
This scenario is very similar to the previous one, but in this case we are specifying that departmentId path variable should be bound to someDepartmentId parameter. This means that we can override the default binding mechanism explained in the previous scenario and define our own mechanism.
Extracting multiple path variables
@RequestMapping(value="/departments/{departmentId}/employees/{employeeId}") public String findEmployee( @PathVariable String departmentId, @PathVariable String employeeId){ System.out.println("Find employee with ID: " + employeeId + " from department: " + departmentId); return "someResult"; }
Testing URL: /departments/23/employees/51
This scenario now specifies multiple path variables in the same URL, each one being bound to a distinct method parameter. This method prints: Find employee with ID: 51 from department: 23.
Extracting regular expressions
@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}") public String regularExpression( @PathVariable String textualPart, @PathVariable String numericPart){ System.out.println("Textual part: " + textualPart + ", numeric part: " + numericPart); return "someResult"; }
Testing URL: /sometext.123
We may also specify regular expression based patterns. The syntax used in the pattern is {pathVar:regularExpression}. As usual the path variables are then bound to the parameters respectively. Given the example url - /sometext.123 - this method prints: Textual part: sometext, numeric part: 123.