When calling a Spring MVC application we generally use a URL which calls the Controller. In the Controller to map a given URL we use RequestMapping annotation –
@RequestMapping
RequestMapping can be used in many ways. Lets see some of most widely used Spring MVC RequestMapping Examples
1. RequestMapping in Class Level
@RequestMapping can be added at Controller Level. This way the URI provided will act as base URI for all other methods in the Controller.
1 2 3 4 5 |
@Controller @RequestMapping(value = "/employee") public class EmployeeController { // Methods.... } |
Now any requests with /employee as URL will hit this Controller class.
2. RequestMapping in Method level
@RequestMapping can also be added in methods of a controller.
1 2 3 4 5 6 7 8 9 10 |
@Controller @RequestMapping(value = "/employee") public class EmployeeController { @RequestMapping(value = "/display") public ModelAndView showEmployeeForm() { //Some Code... } } |
In the code above we have a Class level @RequestMapping /employee and a Method level @RequestMapping /display
So to call the method
showEmployeeForm() we will use
/employee/display. We can have multiple methods in a Controller with @RequestMapping.
3. RequestMapping using @Pathvariable
@RequestMapping can be used to construct dynamic URI, to pass in parameters. This can be achieved using @PathVariable
1 2 3 4 5 6 7 8 9 10 |
@Controller @RequestMapping(value = "/employee") public class EmployeeController { @RequestMapping(value = "/display/{empId}/{empName}") public ModelAndView showEmployeeForm(@PathVariable String empId, @PathVariable String empName) { //Some Code.... } } |
In this case you can pass empID and empName as parameters to the method showEmployeeForm() using
@PathVariable.
example:
/employee/display/123/John
/employee/display/666/Smith
4. RequestMapping using HTTP methods
We have different HTTP methods like POST, GET, DELETE etc. We can call a controller method for each of these methods using @RequestMapping and RequestMethod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@Controller @RequestMapping(value = "/employee") public class EmployeeController { @RequestMapping(value = "/display", method = RequestMethod.GET) public String showEmployeeForm() { //Some Code..... } @RequestMapping(value = "/save", method = RequestMethod.POST) public String saveEmployee() { //Some Code..... } @RequestMapping(value = "/delete", method = RequestMethod.DELETE) public String deleteEmployee() { //Some Code..... } } |
We also can have one Controller method to use more than one RequestMethod. In below code if the URL is /display and HTTP method is either POST or GET, the showEmployeeForm() method will be called.
1 2 3 4 |
@RequestMapping(value = "/display", method = { RequestMethod.GET, RequestMethod.POST }) public String showEmployeeForm() { return null; } |
5. RequestMapping using @RequestParam
In some case we need to pass parameters in a URL or a post request. Similar to the Pathvariable we can get parameters to the method using @RequestParam.
1 2 3 4 |
@RequestMapping(value = "/display", method = RequestMethod.GET) public String showEmployeeForm(@RequestParam("empid") String empId) { //Some Code.... } |
here the URL will be /display?empId=12345