Spring boot @RequestMapping annotation with example

Hey guys in this post, we will discuss spring boot @RequestMapping annotation with an example


@RequestMapping is the most common and widely used annotation in Spring MVC. It is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

It has the following optional options

  • name: Assign a name to this mapping.
  • value:  The primary mapping expressed by this annotation.
  • method: The HTTP request methods to map to
  • headers: The headers of the mapped request, narrowing the primary mapping.
public class UserController {

	public String getUser() {

A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.

This means if you specify the class level annotations, the URL shall be relative, it shall be http://localhost:8080/users/user (URL to Handler mapping) and likewise.

public class UserController {

	public String getUser() {

Now, in this case, the URI path for the getUser() handler method is absolute http://localhost:8080/user

Watch the video

Example on @requestMapping

Now let’s understand this annotation with an example –

Create spring boot project

There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add the maven dependencies

Open pom.xml and add the following maven dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot request mapping example</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.

spring-boot-devtools dependency for automatic reloads or live reload of applications.

Create a controller

Create UserController.java inside the in.bushansirgur.springboot.controller package and add the following content

package in.bushansirgur.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

public class UserController {

	public String getUser() {
		return "getUser()";
	@RequestMapping(value = {"/seconduser", "/second-user", "/secondUser"}, 
					method = RequestMethod.GET)
	public String getSecondUser() {
		return "getSecondUser()";

In the second handler method getSecondUser(), we have specified multiple paths with comma separated, this is equivalent to http://localhost:8080/users/seconduser, http://localhost:8080/users/second-user, and http://localhost:8080/users/secondUser

Run the application

You can run the application by executing the below command

mvn spring-boot:run

Open the browser and type the following URLs and see the output http://localhost:8080/users/user


  • http://localhost:8080/users/seconduser


  • http://localhost:8080/users/second-user


  • http://localhost:8080/user


Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

This Post Has One Comment

  1. chaatsapp

    Bushan, I really appreciate your efforts on this article. It has helped me a lot.

Leave a Reply