Spring MVC @Controller, @RequestMapping, @RequestParam, and @PathVariable Annotation Example


In this tutorial, you will learn Spring MVC @Controller, @RequestMapping, @RequestParam, and @PathVariable annotation example. These annotations are frequently used while developing a Spring MVC based application. In other words, you can say that these annotations are the backbone of the Spring MVC module, it removes the complexity of the XML configuration file.

As the name suggested @Controller annotation declare the class as a controller class, @RequestMapping is used to map the web request to the controller. @RequestMapping annotation is most wildly used annotation in Spring MVC application. @RequestParam fetch the URL parameter and map it to the method argument, and @PathVariable annotation map the URI variable to one of the method arguments.

@Controller Annotation

The @Controller annotation indicates that an annotated class is a “Controller” (e.g. a web controller). It is typically used in combination with annotated handler methods based on the annotation. @Controller annotation is stereotype annotation and its qualified path is org.springframework.stereotype.Controller.

@Controller
public class HelloController {

}

If you want to map this controller with a specific URL, then it will look like…

@Controller
@RequestMapping(value="/india")
public class HelloController {

}

@RequestMapping Annotation

@RequestMapping annotation is most wildly used annotation in Spring MVC module. It is used for mapping web requests onto specific handler classes and/or handler methods. @RequestMapping annotation can be applied for the class as well as methods. The qualified path of @RequestMapping annotation is org.springframework.web.bind.annotation.RequestMapping.

@RequestMapping(value = "/greet")
public ModelAndView namaste() {

	ModelAndView model = new ModelAndView();
	
	model.setViewName("index");

	return model;
}

If you want strict the URL request with specific HTTP method, you can do that with @RequestMapping annotation.

@RequestMapping(value = "/greet", method = RequestMethod.GET)
public ModelAndView namaste() {

	ModelAndView model = new ModelAndView();
	
	model.setViewName("index");

	return model;
}

It supports all HTTP methods.

public enum RequestMethod {

	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE

}

@RequestParam Annotation

@RequestParam annotation which indicates that a method parameter should be bound to a web request parameter or fetch the URL parameters and map it to the method argument. Suppose you want to submit the form data to the controller.

<form action="/greet" method="GET">
	<input type="text" name="president"/>
	<input type="number" name="age"/>
	<input type="submit" value="Submit"/>
</form>

To get the above form data in your controller, you can use the @RequestParam annotation and make sure the input field name must be the same.

@RequestMapping(value = "/greet", method = RequestMethod.GET)
public ModelAndView namaste(@RequestParam("name") String name, @RequestParam("age") int age) {

	ModelAndView model = new ModelAndView();
	model.addObject("data", "President name:" + name + " age:" + age);
	model.setViewName("index");

	return model;
}

@PathVariable Annotation

RequestMapping annotation can be used to handle dynamic URIs where one or more of the URI value works as a parameter. @PathVariable annotation which indicates that a method parameter should be bound to a URI template variable.

@RequestMapping(value = "/welcome/{firstName}/{lastName}")
public ModelAndView welcomeBack(@PathVariable("firstName") String first, @PathVariable("lastName") String last) {

	ModelAndView model = new ModelAndView();
	model.addObject("data", "Welcome Mr." + first + " " + last);
	model.setViewName("index");

	return model;
}

In this case, your URL will be  /welcome/Atul/Rai.

Let’s see the complete example of all above annotation together.

HelloController.java
package org.websparrow.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/india")
public class HelloController {

	@RequestMapping(value = "/greet", method = RequestMethod.GET)
	public ModelAndView namaste(@RequestParam("name") String name, @RequestParam("age") int age) {

		ModelAndView model = new ModelAndView();
		model.addObject("data", "President name:" + name + " age:" + age);
		model.setViewName("index");

		return model;
	}

	@RequestMapping(value = "/welcome/{firstName}/{lastName}")
	public ModelAndView welcomeBack(@PathVariable("firstName") String first, @PathVariable("lastName") String last) {

		ModelAndView model = new ModelAndView();
		model.addObject("data", "Welcome Mr." + first + " " + last);
		model.setViewName("index");

		return model;
	}

}

References

  1. Annotation Type Controller
  2. Annotation Type RequestMapping
  3. Annotation Type RequestParam
  4. Annotation Type PathVariable

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.