Spring MVC @RequestMapping Example

In this tutorial you will learn how to use @RequestMapping annotation to match URL patterns in different scenarios.

In spring MVC @RequestMapping plays the role of mapping URL to the method of controller class.In 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 can be used in many ways.

I am giving some examples of @RequestMapping annotation to show that how you can use @RequestMapping to map URLs to controller methods in different ways.

In this tutorial you will learn how to use @RequestMapping annotation to match URL patterns in different scenarios.

In spring MVC @RequestMapping plays the role of mapping URL to the method of controller class.In 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 can be used in many ways.

I am giving some examples of @RequestMapping annotation to show that how you can use @RequestMapping to map URLs to controller methods in different ways.


1. RequestMapping at Class Level

@RequestMapping can be added at class Level. This way the URI provided will act as base URI for all other methods in the Controller class.

@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
// Methods....

}

Now any requests with /employee as URL will hit this Controller class.

2. RequestMapping only at Method level

@RequestMapping is optional for class level and we can use it to annotate methods only.

@Controller
public class EmployeeController 
{
    @RequestMapping("/employee-management/employees")
    public String getAllEmployees(Model model)
    {
        //application code
        return "employeesList";
    }
     
    @RequestMapping("/employee-management/employees/add")
    public String addEmployee(EmployeeVO employee)
    {
        //application code
        return "employeesDetail";
    }
     
    @RequestMapping("/employee-management/employees/update")
    public String updateEmployee(EmployeeVO employee)
    {
        //application code
        return "employeesDetail";
    }
}

In the above example /employee-management/employees/add will call addEmployee(EmployeeVO employee) method.


3. @RequestMapping annotations at class level as well as method levels

In the below example @RequestMapping is applied to both class and method level.

@Controller
@RequestMapping("/employee-management/employees/*")
public class EmployeeController 
{
    @RequestMapping
    public String getAllEmployees(Model model)
    {
        //application code
        return "employeesList";
    }
     
    @RequestMapping("/add")
    public String addEmployee(EmployeeVO employee)
    {
        //application code
        return "employeesDetail";
    }
     
    @RequestMapping("/update")
    public String updateEmployee(EmployeeVO employee)
    {
        //application code
        return "employeesDetail";
    }

In the above example <BASE_URL>/employee-management/employees/add will call addEmployee(EmployeeVO employee) method.


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.

@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.....
	}
 
}

Also we 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.

@RequestMapping(value = "/display", method = { RequestMethod.GET, RequestMethod.POST })
	public String showEmployeeForm() {
		return null;
	}

5. @RequestMapping with @RequestParam

To fetch query string from the URL, @RequestParam is used as an argument.

@Controller
public class EmployeeController {
	@RequestMapping(value="fetch")
	public String getInfo(@RequestParam("id") String id) {
		System.out.println("id:"+ id);
		return "success";
	}
}  


If we access the URL as http://localhost:8080/sampleapp/fetch?id=KB100 then the id will be accessed by @RequestParam and the output will be KB100.


6. @RequestMapping with @PathVariable

To access path variable, spring provides @PathVariable that is used as an argument. We have to refer the variable in @RequestMapping using {}.

@RequestMapping(value="/fetch/{id}")
public String getInfo(@PathVariable("id") String id) {
   System.out.println("id:"+ id);
   return "success";
}  

If we access the URL as /fetch/200, then the path variable id we will be assigned the value 200.





comments powered by Disqus