VOOZH about

URL: https://www.geeksforgeeks.org/springboot/spring-requestmapping-annotation-with-example/

⇱ Spring @RequestMapping Annotation with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring @RequestMapping Annotation with Example

Last Updated : 24 Apr, 2026

The @RequestMapping annotation in Spring MVC is used to map HTTP requests to specific handler methods or controller classes. It defines the URL path, HTTP method, and other request conditions for handling incoming requests. This helps route client requests to the appropriate business logic in a structured way.

  • Can be applied at both class and method level to define URL patterns
  • Works with GET, POST, PUT, DELETE using attributes or specialized annotations

Real-World Example: In an e-commerce application, @RequestMapping("/products") can map all product-related requests, and methods like @RequestMapping(value="/add", method=POST) handle adding a new product, while /list fetches all available products.

@RequestMapping Annotation at Method Level

Step 1: Create Dynamic Web Project

Create a Dynamic Web Project in STS/Eclipse.

  • Go to File -> New -> Dynamic Web Project
  • Set project name and select Apache Tomcat
  • This sets up a basic web application structure

Step 2: Add Spring JAR Files

Add required Spring framework libraries.

  • Download Spring JARs from repository
  • Place them in /WEB-INF/lib
  • Enables Spring MVC features in the project

Step 3: Configure Tomcat Server

  • Go to Properties -> Targeted Runtimes
  • Select Apache Tomcat
  • Run project using Run on Server

The DispatcherServlet is the front controller in Spring MVC that routes incoming HTTP requests to the appropriate handler methods. Let’s configure it in the web.xml file.

Step 4: Configure web.xml

The DispatcherServlet is the front controller in Spring MVC that routes incoming HTTP requests to the appropriate handler methods. Let’s configure it in the web.xml file.

Go to the src/main/webapp/WEB-INF/web.xml file and add the following configuration:

  • The DispatcherServlet is mapped to /student.com/*, meaning all requests starting with /student.com/ will be handled by Spring MVC controllers.
  • Spring automatically looks for a configuration file named frontcontroller-dispatcher-servlet.xml.

Step 5: Configure frontcontroller-dispatcher-servlet.xml

Go to the src/main/webapp/WEB-INF folder and create an XML file named frontcontroller-dispatcher-servlet.xml. Add the following configuration:

The component-scan ensures that Spring scans the com.student.controllers package for annotated classes.

Step 6: Create Controller Class

Create DemoController in specified package

  • Annotate class with @Controller
  • Defines it as a Spring MVC controller
  • The @RequestMapping("/hello") annotation maps the /hello URL to the helloWorld() method.
  • The @ResponseBody annotation indicates that the return value of the method will be the response body.

Step 7: Run the Application

Right-click on your project and select Run As > Run on Server.

πŸ‘ Image

Use the following URL to access the controller:

http://localhost:8080/myfirst-mvc-project/student.com/hello

Output:

πŸ‘ Image

The class-level @RequestMapping annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

Multi-Action Controller

So in this example, we are going to create Multi-Action Controller. It's a Controller implementation that allows multiple request types to be handled by the same class. That means inside one controller class we can have many handler methods something like this.

Step 8 : Add Class-Level @RequestMapping

  • Add @RequestMapping("/boys") at class level
  • All methods inherit this base path

Step 9 : Add Multiple Handler Methods

Handle multiple requests in same controller.

  • /boys/hello -> returns "Hello World!"
  • /boys/geeksforgeeks -> returns welcome message
  • Improves organization and reusability

Step 10 : Test Updated URLs

Verify correct endpoint usage. 

  • Old URL /student.com/hello
  • Correct URL /student.com/boys/hello
  • /student.com/boys/geeksforgeeks

πŸ‘ Run Your Spring MVC Controller

And now, if you use this "http://localhost:8080/myfirst-mvc-project/student.com/hello" URL to run your controller then you are going to get the following warning and there will be no response

WARNING: No mapping for GET /myfirst-mvc-project/student.com/hello

In order to run your controller, you have to hit the following URL

http://localhost:8080/myfirst-mvc-project/student.com/boys/hello

Similarly, for the welcomeGfgMessage() handler method, you have to hit the following URL

http://localhost:8080/myfirst-mvc-project/student.com/boys/geeksforgeeks

Comment

Explore