Saturday, April 18, 2020

Spring @RequestMapping Annotation Examples

In spring mvc hello world application, we saw a very basic employee management application with end to end functionality (excluding any db access). In next step to learn spring mvc module, 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. I am again using the same code base as in spring mvc hello world application, and changing only controller class using uses @RequestMapping annotation.

1) @RequestMapping annotations at only method level

In this type of usage of @RequestMapping annotations, you must give complete paths as value attribute. e.g. look at below controller code having basic CRUD operations.
@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";
    }
     
    @RequestMapping(value={"/employee-management/employees/remove","/employee-management/employees/delete"})
    public String removeEmployee(@RequestParam("id") String employeeId)
    {
        //application code
        return "employeesList";
    }
}
If there are multiple URLs which can be mapped to single method then you can pass an array of string parameters having all different URLs to value attribute e.g. we did this for removeEmployee() method in above example. This method will be called if you call URL <BASE_URL>/employee-management/employees/remove or <BASE_URL>/employee-management/employees/delete.

2) @RequestMapping annotations at class level as well as method levels

One thing to notice in above example is that “/employee-management/employees” is part of each URL mapped to all methods. It will be good if we have put it in some common pace and each method should have only required piece of identifier.
This can be done by putting @RequestMapping annotation at class level and method level both. Look at below example.
@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";
    }
     
    @RequestMapping(value={"/remove","/delete"})
    public String removeEmployee(@RequestParam("id") String employeeId)
    {
        //application code
        return "employeesList";
    }
}
Now we have applied annotation at class level as well. Note that this change does not change the behavior of mappings. They are exactly same as they were before.
Another thing to note is that first method getAllEmployees() lacks a URL value. Since the class level uses the /employee-management/employees/* URL wildcard so this handler method is executed as a catch block if no other matched for any request. So any URL request (e.g., /employee-management/employees/list or /employee-management/employees/abcd or /employee-management/employees/) triggers this method.

3) @RequestMapping annotations using only HTTP request types

This is also possible that you can have only one @RequestMapping annotation at class level and at method levels you don’t specify ay URL value. Just specify the HTTP request types so that each different http type is mapped to different method. This type of design is very popular in RESTFul webservices.
@Controller
@RequestMapping("/employee-management/employees")
public class EmployeeController
{
    @RequestMapping (method =  RequestMethod.GET)
    public String getAllEmployees(Model model)
    {
        //application code
        return "employeesList";
    }
     
    @RequestMapping (method =  RequestMethod.POST)
    public String addEmployee(EmployeeVO employee)
    {
        //application code
        return "employeesDetail";
    }
     
    @RequestMapping (method =  RequestMethod.PUT)
    public String updateEmployee(EmployeeVO employee)
    {
        //application code
        return "employeesDetail";
    }
     
    @RequestMapping (method =  RequestMethod.DELETE)
    public String removeEmployee(@RequestParam("id") String employeeId)
    {
        //application code
        return "employeesList";
    }
}
Note that in this example code, all methods will be accessed in same URL but with different http request types.
If you want to play with above examples of @RequestMapping annotation, then you can download the sourcecode here.

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...