Spring MVC 4 - @RequestMapping annotation example

Posted on March 22, 2017

Technologies used:   JDK 1.8.0_121 | Spring 4.3.7.RELEASE | Maven 3.3.9 | Apache Tomcat 7.0.47 | Eclipse Mars.2 (4.5.2)

The @RequestMapping annotation is used to map the web request onto a handler class (i.e. Controller) or a handler method

The value attribute of the @RequetMapping annotation is used to map the handler method to a path and it can be written as   @GetMapping(value="/one") , which is equivalent to @GetMapping("/one").

Spring also provides five method-level composed variant of the @RequestMapping annotation -

  • @GetMapping → equivalent to @RequestMapping(method = RequestMethod.GET).
  • @PostMapping → equivalent to @RequestMapping(method = RequestMethod.POST).
  • @PutMapping → equivalent to @RequestMapping(method = RequestMethod.PUT).
  • @DeleteMapping → equivalent to @RequestMapping(method = RequestMethod.DELETE).
  • @PatchMapping → equivalent to @RequestMapping(method = RequestMethod.PATCH).

The following examples demonstrate how the @RequestMapping annotation works in the Spring MVC application.

Controller class

Create a @Controller class annotated with @RequestMapping annotation. 

Annotate all handler methods of controller class with @RequestMapping annotation as follows.

Note – The @RequestMapping annotation on the class is not mandatory.


package com.boraji.tutorial.spring.controller;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

 * @author imssbora
public class MyController {
   // This handler method work as default handler
   @RequestMapping(method = RequestMethod.GET)
   public String index(Model model) {
      // Java 8 LocalDate
      DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
      LocalDate date = LocalDate.now();
      model.addAttribute("date", date.format(formatter));
      return "index";

   // Handle GET or POST request
   public @ResponseBody String handlerOne() {
      return "<h1>Inside handlerOne() method of MyController</h1>";

   // Handle POST request only
   @RequestMapping(value = "/two", method = RequestMethod.POST)
   public @ResponseBody String handlerTwo() {
      return "Inside handlerTwo() method of MyController";

   // Handle GET request only
   @RequestMapping(value = "/three", method = RequestMethod.GET)
   public @ResponseBody String handlerThree() {
      return "Inside handlerThree() method of MyController";

   // Handle POST request if the request header
   // contains 'content-type=application/x-www-form-urlencoded'
   @RequestMapping(value = "/four", method = RequestMethod.POST, headers = {
         "content-type=application/x-www-form-urlencoded" })
   public @ResponseBody String handlerFour() {
      return "Inside handlerFour() method of MyController";

   // Handle POST request if the request content type
   // is 'application/x-www-form-urlencoded'
   @RequestMapping(value = "/five", method = RequestMethod.POST, consumes = {
   public @ResponseBody String handlerFive() {
      return "Inside handlerFive() method of MyController";

   // Handle POST or GET request and produce content type of "text/plain"
   @RequestMapping(value = "/six", produces = { MediaType.TEXT_PLAIN_VALUE })
   public @ResponseBody String handlerSix() {
      return "Inside handlerSix() method of MyController";

   // Equivalent to @RequestMapping(value="/seven",method=RequestMethod.GET)
   @GetMapping(value = "/seven")
   public @ResponseBody String handlerSeven() {
      return "Inside handlerSeven() method of MyController";

   // Equivalent to @RequestMapping(value="/eight",method=RequestMethod.POST)
   public @ResponseBody String handlerEight() {
      return "Inside handlerEight() method of MyController";

JSP view

Create an index.jsp file under src/main/webapp/WEB-INF/views folder as follows.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - @RequestMapping Example | BORAJI.COM</title>
<style type="text/css">
form {
    display: inline-block;
	<h1>Spring 4 MVC - @RequestMapping example</h1>
	<h4>Server current time is : ${date} </h4>
	<!-- Request One -->
	<form action="/one">
		<input type="submit" value="One">
	<!-- Request Two -->
	<form action="/two" method="post">
		<input type="submit" value="Two">
	<!-- Request Three -->
	<form action="/three" method="get">
		<input type="submit" value="Three">
	<!-- Request Four -->
	<form action="/four" method="post">
		<input type="submit" value="Four">
	<!-- Request Five -->
	<form action="/five" method="post" >
		<input type="submit" value="Five">
	<!-- Request Six -->
	<form action="/six" method="post" >
		<input type="submit" value="Six">
	<!-- Request Seven -->
	<form action="/seven" method="get" >
		<input type="submit" value="Seven">
	<!-- Request Eight -->
	<form action="/eight" method="post" >
		<input type="submit" value="Eight">


Spring configuration

Create a web @Configuration class annotated with @EnableWebMvc and @ComponentScan as follows.


package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

 * @author imssbora

@ComponentScan(basePackages = { "com.boraji.tutorial.spring.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

   public InternalResourceViewResolver resolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      return resolver;

Servlet container initialization

Create a container initializer class by extending the AbstractAnnotationConfigDispatcherServletInitializer class as follows.


package com.boraji.tutorial.spring.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 * @author imssbora
public class MyWebAppInitializer extends 

   protected Class<?>[] getRootConfigClasses() {
      return new Class[]{};

   protected Class<?>[] getServletConfigClasses() {
      return new Class[]{WebConfig.class};

   protected String[] getServletMappings() {
      return new String[]{"/"};

Build + Deploy + Run application

Use the following maven commands to build, deploy and run embedded Tomcat server.

mvn clean install  (This command triggers war packaging)

mvn tomcat7:run (This command run embedded tomcat and deploy war file automatically)

You can refer this link to learn how to run the above commands in Eclipse IDE.

Now, open web browser and type URL http://localhost:8080/ in address bar. This URL will render the index.jsp as follows.Spring-mvc-request-mapping01.png

Click on the button One in the main page. This will send the GET request to the handlerOne() method in the controller class.


Click on the button Two in the main page. This will send the POST request to the handlerTwo() method in the controller class.


Click on the button Three in the main page. This will send the GET request to the handlerThree () method in the controller class.


Similarly you can run and test the other request URLs as specified in the controller class.