codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

12 / 62 topics
11Creating RESTful Controllers12Request Mapping and HTTP Methods13Using Path Variables and Query Parameters14Using ResponseEntity for HTTP Responses
Tutorials/Spring Boot/Request Mapping and HTTP Methods
🍃Spring Boot

Request Mapping and HTTP Methods

Updated 2026-05-15
10 min read

Request Mapping and HTTP Methods

Introduction

In the world of web development, handling HTTP requests is a fundamental task. Spring Boot provides a powerful and flexible way to map incoming HTTP requests to specific methods in your controller classes using annotations like @RequestMapping. This tutorial will guide you through understanding how to use these annotations effectively.

Concept

The core annotation used for mapping requests is @RequestMapping, which can be applied at both the class level and the method level. When applied at the class level, it specifies a base URL path that all methods in the controller should inherit. At the method level, it specifies how specific HTTP methods (GET, POST, PUT, DELETE) should be handled.

Spring Boot also provides more specific annotations like @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping for mapping requests to specific HTTP methods. These are essentially shorthand for using @RequestMapping with the method attribute set to a specific HTTP method.

Examples

Basic Request Mapping

Let's start by creating a simple Spring Boot application that maps different HTTP methods to controller methods.

  1. Create a new Spring Boot project: You can use Spring Initializr to generate a new Spring Boot project with the necessary dependencies (e.g., Spring Web).

  2. Add a Controller Class: Create a new Java class in your src/main/java/com/example/demo directory.

Java
1package com.example.demo;
2
3import org.springframework.web.bind.annotation.GetMapping;
4import org.springframework.web.bind.annotation.PostMapping;
5import org.springframework.web.bind.annotation.PutMapping;
6import org.springframework.web.bind.annotation.DeleteMapping;
7import org.springframework.web.bind.annotation.RestController;
8
9@RestController
10public class RequestMappingController {
11
12 @GetMapping("/hello")
13 public String sayHello() {
14 return "Hello, GET request!";
15 }
16
17 @PostMapping("/hello")
18 public String createHello() {
19 return "Hello, POST request!";
20 }
21
22 @PutMapping("/hello")
23 public String updateHello() {
24 return "Hello, PUT request!";
25 }
26
27 @DeleteMapping("/hello")
28 public String deleteHello() {
29 return "Hello, DELETE request!";
30 }
31}
  1. Run the Application: You can run your Spring Boot application using your IDE or by executing the following command in the terminal:
Terminal
Output
Hello, GET request!
Hello, POST request!
Hello, PUT request!
Hello, DELETE request!

Using @RequestMapping

You can also use the @RequestMapping annotation to achieve the same functionality.

Java
1package com.example.demo;
2
3import org.springframework.web.bind.annotation.RequestMapping;
4import org.springframework.web.bind.annotation.RequestMethod;
5import org.springframework.web.bind.annotation.RestController;
6
7@RestController
8@RequestMapping("/api")
9public class RequestMappingController {
10
11 @RequestMapping(value = "/hello", method = RequestMethod.GET)
12 public String sayHello() {
13 return "Hello, GET request!";
14 }
15
16 @RequestMapping(value = "/hello", method = RequestMethod.POST)
17 public String createHello() {
18 return "Hello, POST request!";
19 }
20
21 @RequestMapping(value = "/hello", method = RequestMethod.PUT)
22 public String updateHello() {
23 return "Hello, PUT request!";
24 }
25
26 @RequestMapping(value = "/hello", method = RequestMethod.DELETE)
27 public String deleteHello() {
28 return "Hello, DELETE request!";
29 }
30}

Combining Class and Method Level Mappings

You can also combine class-level and method-level mappings to create more complex URL structures.

Java
1package com.example.demo;
2
3import org.springframework.web.bind.annotation.GetMapping;
4import org.springframework.web.bind.annotation.RequestMapping;
5import org.springframework.web.bind.annotation.RestController;
6
7@RestController
8@RequestMapping("/api")
9public class RequestMappingController {
10
11 @GetMapping("/hello")
12 public String sayHello() {
13 return "Hello, GET request!";
14 }
15
16 @RequestMapping(value = "/greet", method = RequestMethod.POST)
17 public String createGreeting() {
18 return "Greetings, POST request!";
19 }
20}

What's Next?

In the next section, we will explore how to use path variables and query parameters in your controller methods. This will allow you to handle more dynamic and flexible URL structures.

Stay tuned for more tutorials on Spring Boot!


PreviousCreating RESTful ControllersNext Using Path Variables and Query Parameters

Recommended Gear

Creating RESTful ControllersUsing Path Variables and Query Parameters