In the previous tutorial, we have discussed the role of a Dispatcher Servlet and the steps required to create a Spring MVC application. In this tutorial, we will discuss different type of request mapping to Spring controllers.

Dispatcher Servlet is used to handle all incoming requests and route them through Spring. When DispatcherServlet receives a web request, it determines which controllers should handle the incoming request.

It first scans for all classes that are declared with the @Controller annotation. The dispatching process depends on the various @RequestMapping annotations declared in a controller class and its handler methods. There are three levels of request mapping can be defined in Spring controller.

1. Handler level mapping

The simplest strategy for using @RequestMapping annotations is to decorate the handler methods directly. In this method, you need to declare mapping for each handler method with the @RequestMapping annotation containing a URL pattern. If a handler’s @RequestMapping annotation matches the request URL, DispatcherServlet it dispatches the request to this handler for it to process the request.

Following are the different mapping types supported.

  • By path
    @RequestMapping(“path”)
  • By HTTP method
    @RequestMapping(“path”, method=RequestMethod.GET). Other Http methods such as POST, PUT, DELETE, OPTIONS, and TRACE are are also supported.
  • By query parameter
    @RequestMapping(“path”, method=RequestMethod.GET, params=”param1”)
  • By presence of request header
    @RequestMapping(“path”, header=”content-type=text/*”)

The request mapping for methods can be defined as follows:

@Controller
public class Employee {

	@RequestMapping("/employee/add")
	public ModelAndView add(
			@RequestParam(value = "firstName") String firstName,
			@RequestParam(value = "surName") String surName) {
		//....
		//....
		return null;
	}
	
	@RequestMapping(value={"/employee/remove","/employee/delete"})
	public ModelAndView delete(
			@RequestParam(value = "uuid") String uuid) {
		//....
		//....
		return null;
	}	
}

In the above code snippet, the controller add() method is declared with @RequestMapping("/employee/add"). If the incoming request path to matches is /employee/add, then the add() handler method will be invoked to process the request.

Handler mappings match URLs according to their paths relative to the context path (i.e., the path where the web application is deployed) and the servlet path (i.e., the path mapped to DispatcherServlet).

2. Mapping at Controller class level

The @RequestMapping annotation can also be used to decorate a controller class. This is helpful to take the control at top level and filter the incoming requests. If the incoming request matches the pattern defined in controller class, then it search the controller methods mappings.

The following code snippet describes how to define Spring @RequestMapping at controller class level.

@Controller
@RequestMapping("/employee/*")
public class Employee {

	@RequestMapping("add")
	public ModelAndView add(
			@RequestParam(value = "firstName") String firstName,
			@RequestParam(value = "surName") String surName) {
		//....
		//....
		return null;
	}
	
	@RequestMapping(value={"remove","delete"})
	public ModelAndView delete(
			@RequestParam(value = "uuid") String uuid) {
		//....
		//....
		
		return null;
	}	
}

Notice that, we have used the wildcard (*) for the @RequestMapping annotation for broader URL matching.

3. Mapping requests by request type

The @RequestMapping annotation handles all types of incoming HTTP request including GET, POST, PUT, DELETE, PATCH etc. By default, it’s assumed all incoming requests to URLs are of the HTTP GET kind. To differentiate the mapping by HTTP request type, we need to explicitly specify the HTTP request method in the @RequestMapping declaration.

The following code snippet describes how to declare Spring mappings by HTTP request methods.

@Controller
@RequestMapping("/employee/*")
public class Employee {

	@RequestMapping(value = { "remove", "delete" }, method = { RequestMethod.POST, 	RequestMethod.DELETE })
		public ModelAndView delete(@RequestParam(value = "uuid") String uuid) {
			// ....
			// ....
	
			return null;
		}
}
This article is about: MVC,

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.