The @RequestMapping annotation is used at class level to map the /calculation URI to the CalculationController class; that is, it ensures that the HTTP request to /calculation is mapped to the CalculationController class. Based on the path defined using the @RequestMapping annotation of the URI (postfix of /calculation, for example, /calculation/sqrt/144), it would be mapped to the respective methods. Here, the request mapping, /calculation/sqrt, is mapped to the sqrt() method, and /calculation/power is mapped to the pow() method.
You might have also observed that we have not defined what request method (GET/POST/PUT, and so on) these methods would use. The @RequestMapping annotation maps all the HTTP request methods by default. You could use specific methods by using the method property of RequestMapping. For example, you could write a @RequestMethod annotation in the following way to use the POST method:
xxxxxxxxxx
@RequestMapping(value = "/power", method = POST)
xxxxxxxxxx
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") Employee employee) {
// Code that uses the employee object
return "employeeView";
}
xxxxxxxxxx
@RestController
@RequestMapping("/home")
public class IndexController {
@RequestMapping("/")
String get(){
//mapped to hostname:port/home/
return "Hello from get";
}
@RequestMapping("/index")
String index(){
//mapped to hostname:port/home/index/
return "Hello from index";
}
}
xxxxxxxxxx
@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";
}
}