Spring boot @RequestMapping annotation with example





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

Overview


@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.
@Controller
@RequestMapping("/users")
public class UserController {

	@RequestMapping("/user")
	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.

@Controller
public class UserController {

	@RequestMapping("/user")
	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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>requestmapping</artifactId>
	<version>v1</version>
	<name>requestmapping</name>
	<description>Spring boot request mapping example</description>
	<properties>
		<java.version>1.8</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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>

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;

@RestController
@RequestMapping("/users")
public class UserController {

	@RequestMapping("/user")
	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

Screenshot-2021-02-18-at-6-03-22-PM

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

Screenshot-2021-02-18-at-6-03-35-PM

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

Screenshot-2021-02-18-at-6-03-49-PM

  • http://localhost:8080/user

Screenshot-2021-02-18-at-6-04-10-PM



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