Spring Boot @RequestMapping Annotation Explained

Hi everyone 👋 Continuing the Spring Boot Annotation Series, today let’s understand one of the most important web annotations 👇 📌 Spring Boot Annotation Series Part 16– @RequestMapping The @RequestMapping annotation is used to map HTTP requests(like GET, POST etc) to handler methods in a controller. It is part of Spring Framework and is mainly used in Spring MVC. 🔹 What does @RequestMapping do? It defines: - URL path - HTTP method (GET, POST, etc.) - Request parameters - Headers - Consumes / Produces (Content-Type) 🔹 Basic Example - @RestController @RequestMapping("/api") public class UserController { @RequestMapping("/users") public String getUsers() { return "List of users"; } } 👉 This maps to: http://localhost:8080/api/users 🔹 Specifying HTTP Method - @RequestMapping(value = "/users", method = RequestMethod.GET) public String getUsers() { return "Users"; } But nowadays we prefer: @GetMapping("/users") 🔹 Where Can We Use It? ✅ At Class Level → Base URL ✅ At Method Level → Specific endpoint Example: @RequestMapping("/api") // class level @GetMapping("/users") // method level Final URL → /api/users 🔹 In Simple Words @RequestMapping connects a URL to a controller method. It tells Spring: “When this request comes, execute this method.” #SpringBoot #Java #SpringMVC #BackendDevelopment #InterviewPreparation #LearningInPublic

To view or add a comment, sign in

Explore content categories