Spring MVC -RequestMapping

Spring MVC - @RequestMapping

@RequestMapping is one of the most widely used Spring MVC annotation. It is used to map web requests onto specific handler classes and/or handler methods.

@RquestMapping annontation can be used in following Levels

1.@RequestMapping –at Class level

If you declare @RequestMapping at the class level, the path will be applicable to all the methods in the class.

@Controller
@RequestMapping(value = "/student")
public class StudentController {
	public ModelAndView addStudent(Student student) {
 return new ModelAndView("addPage", "msg", "Student Added");
	}
}

here /“/student is enforced to all the methods inside the class. Here we can pass multiple urls to value attribute like

2.@RequestMapping –at Method Level

@Controller
@RequestMapping(value = "/student")
public class StudentController {
	@RequestMapping(value = "/add")
	public ModelAndView addStudent(Student student) {
 return new ModelAndView("addPage", "msg", "Student Added");
	}
}

Here /add path is applied at method level. To access the addStudent(-) method URL should be ClassURL+MethodUrl = “/student/add

3.@RequestMapping –at HTTP Method Level

Here HTTP methods will filter the handler mappings

@Controller
@RequestMapping(value = "/student")
public class StudentController {
    
    @RequestMapping(value = "/add" method=RequestMethod.GET)
    public ModelAndView addStudent(Student student) {        
        return new ModelAndView("addPage", "msg", "Student Added");        
    }
    @RequestMapping(value = "/add" method=RequestMethod.POST)
    public ModelAndView addStudent(Student student) {        
        return new ModelAndView("addPage", "msg", "Student Added");        
    }    
}

In the above code, if you look at the first two methods mapping to the same URI, but both have the different HTTP methods. First method will be invoked when HTTP method GET is used and the second method is invoked when HTTP method POST is used.

4.@RequestMapping –Using ‘params’

Here the parameters in the query string will filter the handler mappings.

@Controller
@RequestMapping(value="/student")
public class HelloWorldController {
@RequestMapping(value="/fetch", params ="sno" )
	public String getSno(@RequestParam("sno") String sno) {
 return "success";
	}
	@RequestMapping(value="/fetch", params = "name")
	public String getName(@RequestParam("name") String name) {
 	return "success";
	}
	@RequestMapping(value="/fetch", params = {"sno=200","name=satya"})
    	public String getBoth(@RequestParam("id") String id, @RequestParam("name") String n) {
 	return "success";
	}
}
  • if request is /student/fetch? sno=100 then getSno(-) will execute.

  • if request is /student/fetch? name=satya then getName(-) will execute.

  • if request is /student/fetch? sno=100&name=satya then getBoth(-,-) will execute.

5. @RequestMapping –Working with Parameters

We have two annotations to process the parameters in given URL. They are

  • @RequestParam

  • @PathVariable

@RequestParam

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

URL: /student/fetch? sno=100&name=satya

@Controller
@RequestMapping(value="/student")
public class HelloWorldController {
	@RequestMapping(value="/fetch")
    	public String getBoth(@RequestParam("id") String id, @RequestParam("name") String n) {
System.out.println(-Sno: "+sno+-, Name : "+n)
 	return "success";
	}
}

@PathVariable

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

URL: /student/fetch/100/satya

@RequestMapping(value="/fetch/{sno}/{name}")
public String getInfo(@PathVariable("sno") String sno, (@PathVariable("sno") String n ) {
System.out.println(-Sno:"+sno+-, Name : "+n)
   	return "success";
}

@RequestMapping for Fallback
Using @RequestMapping, we can implement a fallback method. For every response file not found exception, this method will be called, in this way we can implement 404 response.

@RequestMapping(value="*")
public String default() {
    return "success";
}