Spring Boot - @Requestmapping
Last Updated :
23 Jul, 2025
Spring Boot is the most popular framework of Java for building enterprise-level web applications and back-ends. Spring Boot has a handful of features that support quicker and more efficient web app development. Some of them are Auto-configuration, Embedded Server, opinionated defaults, and Annotation Support. In this article, we'll be exploring the core annotation of Spring Boot - @RequestMapping which is part of the set of annotations that Spring Boot employs for defining URL endpoints and REST APIs.
@RequestMapping
This annotation is a versatile and flexible annotation that can be used with a controller (class) as well as the methods to map specific web requests with the handler methods and controllers. This annotation is part of a larger set of annotations provided by Spring Framework to define URL endpoints and simplify the development of Spring Boot applications.
It has the following features:
- Define several different endpoints to access a specific resource.
- Build REST APIs to serve web requests.
- Simplify the web development process by simply defining an annotation that offers a set of functionalities for handling requests.
- Define multiple endpoints in a single @RequestMapping annotation.
Step-By-Step Implementation of @RequestMapping annotation
For this article, we'll be using the following tools:
- Java 8 or higher
- Java IDE like Eclipse, IntelliJ, and VS code (We'll be using IntelliJ)
- POSTMAN for testing Request Mappings
- Dependency: Spring Web Starter
Step-1: Create a starter file and extract it
Go to Spring Initializr and create a starter file having a single dependency - Spring Web. Download and extract it into your local folder and open it in your favorite IDE.

pom.xml File:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.GeeksForGeeks</groupId>
<artifactId>RequestMappingExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RequestMappingExample</name>
<description>Request Mapping Example</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step-2: Define a Controller
- Create a new package for containing all the controllers that we will be adding in our application under the src/main/java/package_name/Controllers
- Add a new TestController inside Controllers Package for defining Request Mappings. Your final directory structure would look something like this :

Step-4: Define URL Templates inside the controller
Annotate the Controller with @Controller to signify that this class is a controller that has some URL templates defined inside it and @RequestMapping on the controller as well as the methods to specify which URL path will give what output.
Below is the code Implementation of Controller:
Java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
// Controller class
@Controller
@ResponseBody
@RequestMapping("/test")
public class TestController {
// URL Path - 1
// Returns a String
@RequestMapping("/hello")
public String sayHello() {
System.out.println("dsvsdvdsvs");
return "Hello Geek!";
}
// URL Path - 2
// Returns a String
@RequestMapping("/sport")
public String doSomeSport() {
return "Run 5 kilometers today!";
}
// URL Path - 3
// Returns a List
@RequestMapping("/today/tasks")
public List<String> todaysTasks() {
List<String> myTasks = new ArrayList<String>();
myTasks.add("Write 5 articles on GeeksforGeeks Today");
myTasks.add("Run 5 kilometers");
myTasks.add("Do Laundry");
return myTasks;
}
}
Explanation of the above Program:
We've defined multiple end points using @RequestMapping which can be accessed at following URLs :
- http://localhost:8080/test/hello
- http://localhost:8080/test/sport
- http://localhost:8080/test/today/tasks
Annotations used:
- @Controller: This annotation implicitly marks the class as a component making it eligible for component scanning while signifying that this class invokes business logic, handles incoming web requests and returns customized responses.
- @ResponseBody: This annotation is used on both class level as well as on method level to indicate that the return value generated by this class or method doesn't need to be resolved to a view page like HTML or JSP, rather it should be directly serialized into HTTP response body. And this serialization from Java Object to JSON is done by a technology called - Jackson Project that performs Jackson Data Binding under the hood.
Our request method can return any type of data - POJOs (Plain Old Java Objects) and this data will be serialized into JSON by the Jackson Project.
Note: @ResponseBody can be ignored if you're using @RestController instead of @Controller.
Outputs for every endpoint on POSTMAN:
Endpoint - 1: http://localhost:8080/test/hello
Endpoint - 2: http://localhost:8080/test/sport

Endpoint - 3: http://localhost:8080/test/today/tasks

Notice that we're selecting the type of request as GET in our POSTMAN this is because all of our methods are only returning some data, not making any changes in a database (as we're not connected to any database).
And to be more specific, the actual use of @RequestMapping is to define the start of URL and the request handler method will perform some operation and depending on the type of operation a method performs, we will annotate it with GET, PUT, POST or DELETE. Let's look at some of the most commonly used type of annotation we can use with a specific web request in brief:
- @GetMapping: This annotation is associated with the requests that are requesting a resource without making any changes in the database.
- @PostMapping: This annotation is associated with the requests that are trying to add new data in the database. Ex: Adding a new student record that contains all the information regarding a student like student_id, name, enrollment_number, branch etc.
- @PutMapping: This annotation is associated with the update requests that wants to update or change some already existing data in our database.
- @DeleteMapping: This annotation is associated with deleting persistent objects (already existing data) from our database.
Conclusion
In conclusion, @RequestMapping is a core annotation for defining a controller and mapping web requests to their appropriate methods. It is a core MVC Annotations defined for defining the controller and URL path, it is very crucial to understand the working and different variations of this annotation as a Java Developer you'll be using it very frequently.
- It maps certain web requests to their specific controller and methods.
- We will have multiple controllers in our application, then each controller's URL pattern will be different to identify and map web requests to an appropriate controller. For Example, we can have a controller for performing database operation, another controller for Throwing Exceptions, another for logging, monitoring etc. Each of the controller's request mapping will have a unique URL Pattern
- @RequestMapping can also be used to take query parameters from the URL. For example, we need a student with a specific ID. So, we can define a mapping like this - https://localhost:8080/student/{id}. The {id} is a dynamic parameter whose input can vary depending on the requests. Curly braces around it indicates that the input in this part of URL will vary.
- Although @RequestMapping can be annotated on top of Methods as well, it's more efficient and readable to use a specific type of mapping annotation like @GET, @PUT, @POST etc.
Similar Reads
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Spring Boot Basics and Prerequisites
Introduction to Spring BootSpring is one of the most popular frameworks for building enterprise applications, but traditional Spring projects require heavy XML configuration, making them complex for beginners.Spring Boot solves this problem by providing a ready-to-use, production-grade framework on top of Spring. It eliminate
4 min read
Difference between Spring and Spring BootSpring Spring is an open-source lightweight framework that allows Java developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier
4 min read
Spring - Understanding Inversion of Control with ExampleSpring IoC (Inversion of Control) Container is the core of the Spring Framework. It creates and manages objects (beans), injects dependencies and manages their life cycles. It uses Dependency Injection (DI), based on configurations from XML files, Java-based configuration, annotations or POJOs. Sinc
6 min read
Spring - IoC ContainerThe Spring framework is a powerful framework for building Java applications. It can be considered a collection of sub-frameworks, also referred to as layers, such as Spring AOP, Spring ORM, Spring Web Flow, and Spring Web MVC. We can use any of these modules separately while constructing a Web appli
2 min read
BeanFactory vs ApplicationContext in SpringThe Spring Framework provides two core packages that enable Inversion of Control (IoC) and Dependency Injection (DI):org.springframework.beansorg.springframework.contextThese packages define Spring containers that manage the lifecycle and dependencies of beans.Spring offers two main containers1. Bea
6 min read
Spring Boot Core
Spring Boot - ArchitectureSpring Boot is built on top of the Spring Framework and follows a layered architecture. Its primary goal is to simplify application development by providing auto-configuration, embedded servers and a production-ready environment out of the box.The architecture of Spring Boot can be divided into seve
2 min read
Spring Boot - AnnotationsAnnotations in Spring Boot are metadata that simplify configuration and development. Instead of XML, annotations are used to define beans, inject dependencies and create REST endpoints. They reduce boilerplate code and make building applications faster and easier. Core Spring Boot Annotations 1. @Sp
5 min read
Spring Boot ActuatorDeveloping and managing an application are the two most important aspects of the applicationâs life cycle. It is very important to know what is going on beneath the application. Also, when we push the application into production, managing it gradually becomes critically important. Therefore, it is a
5 min read
How to create a basic application in Java Spring BootSpring Boot is the most popular Java framework that is used for developing RESTful web applications. In this article, we will see how to create a basic Spring Boot application.Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also p
3 min read
Spring Boot - Code StructureThere is no specific layout or code structure for Spring Boot Projects. However, there are some best practices followed by developers that will help us too. You can divide your project into layers like service layer, entity layer, repository layer,, etc. You can also divide the project into modules.
3 min read
Spring Boot - SchedulingSpring Boot provides the ability to schedule tasks for execution at a given time period with the help of @Scheduled annotation. This article provides a step by step guideline on how we can schedule tasks to run in a spring boot application Implementation:It is depicted below stepwise as follows:Â St
4 min read
Spring Boot - LoggingLogging in Spring Boot plays a vital role in Spring Boot applications for recording information, actions, and events within the app. It is also used for monitoring the performance of an application, understanding the behavior of the application, and recognizing the issues within the application. Spr
8 min read
Exception Handling in Spring BootException handling in Spring Boot helps deal with errors and exceptions present in APIs, delivering a robust enterprise application. This article covers various ways in which exceptions can be handled and how to return meaningful error responses to the client in a Spring Boot Project. Key Approaches
8 min read
Spring Boot with REST API
Spring Boot - Introduction to RESTful Web ServicesRESTful Web Services REST stands for REpresentational State Transfer. It was developed by Roy Thomas Fielding, one of the principal authors of the web protocol HTTP. Consequently, REST was an architectural approach designed to make the optimum use of the HTTP protocol. It uses the concepts and verbs
5 min read
Spring Boot - REST ExampleIn modern web development, most applications follow the Client-Server Architecture. The Client (frontend) interacts with the server (backend) to fetch or save data. This communication happens using the HTTP protocol. On the server, we expose a bunch of services that are accessible via the HTTP proto
4 min read
How to Create a REST API using Java Spring Boot?Representational State Transfer (REST) is a software architectural style that defines a set of constraints for creating web services. RESTful web services allow systems to access and manipulate web resources through a uniform and predefined set of stateless operations. Unlike SOAP, which exposes its
4 min read
How to Make a Simple RestController in Spring Boot?A RestController in Spring Boot is a specialized controller that is used to develop RESTful web services. It is marked with the @RestController annotation, which combines @Controller and @ResponseBody. This ensures that the response is automatically converted into JSON or XML, eliminating the need f
2 min read
JSON using Jackson in REST API Implementation with Spring BootWhen we build REST APIs with Spring Boot, we need to exclude NULL values from the JSON responses. This is useful when we want to optimize the data being transferred, making the response more compact and easier to process for the client.In this article, we are going to learn the approach that is used
4 min read
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot Kafka Producer ExampleSpring Boot is one of the most popular and most used frameworks of Java Programming Language. It is a microservice-based framework and to make a production-ready application using Spring Boot takes very less time. Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applica
3 min read
Spring Boot Kafka Consumer ExampleSpring Boot is one of the most popular and most used frameworks of Java Programming Language. It is a microservice-based framework and to make a production-ready application using Spring Boot takes very less time. Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applica
3 min read
Spring Boot | How to consume JSON messages using Apache KafkaApache Kafka is a stream processing system that lets you send messages between processes, applications, and servers. In this article, we will see how to publish JSON messages on the console of a Spring boot application using Apache Kafka. In order to learn how to create a Spring Boot project, refer
3 min read
Spring Boot | How to consume string messages using Apache KafkaApache Kafka is a publish-subscribe messaging queue used for real-time streams of data. A messaging queue lets you send messages between processes, applications, and servers. In this article we will see how to send string messages from apache kafka to the console of a spring boot application. Appro
3 min read
Spring Boot | How to publish String messages on Apache KafkaApache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send string messages to Apache Kafka in a spring boot application. In order to learn how to create a spring boot project, r
2 min read
Spring Boot | How to publish JSON messages on Apache KafkaApache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send JSON messages to Apache Kafka in a spring boot application. In order to learn how to create a spring boot project, ref
4 min read
Spring Boot with AOP
Spring Boot - AOP(Aspect Oriented Programming)The Java applications are developed in multiple layers, to increase security, separate business logic, persistence logic, etc. A typical Java application has three layers namely they are Web layer, the Business layer, and the Data layer. Web layer: This layer is used to provide the services to the e
4 min read
How to Implement AOP in Spring Boot Application?AOP(Aspect Oriented Programming) breaks the full program into different smaller units. In numerous situations, we need to log, and audit the details as well as need to pay importance to declarative transactions, security, caching, etc., Let us see the key terminologies of AOP Aspect: It has a set of
10 min read
Spring Boot - Difference Between AOP and OOPAOP(Aspect-Oriented Programming) complements OOP by enabling modularity of cross-cutting concerns. The Key unit of Modularity(breaking of code into different modules) in Aspect-Oriented Programming is Aspect. one of the major advantages of AOP is that it allows developers to concentrate on business
3 min read
Spring Boot - Difference Between AOP and AspectJSpring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
3 min read
Spring Boot - Cache ProviderThe Spring Framework provides support for transparently adding caching to an application. The Cache provider gives authorization to programmers to configure cache explicitly in an application. It incorporates various cache providers such as EhCache, Redis, Guava, Caffeine, etc. It keeps frequently a
6 min read