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.
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.
Let's start by creating a simple Spring Boot application that maps different HTTP methods to controller methods.
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).
Add a Controller Class:
Create a new Java class in your src/main/java/com/example/demo directory.
1package com.example.demo;23import 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;89@RestController10public class RequestMappingController {1112@GetMapping("/hello")13public String sayHello() {14return "Hello, GET request!";15}1617@PostMapping("/hello")18public String createHello() {19return "Hello, POST request!";20}2122@PutMapping("/hello")23public String updateHello() {24return "Hello, PUT request!";25}2627@DeleteMapping("/hello")28public String deleteHello() {29return "Hello, DELETE request!";30}31}
Hello, GET request! Hello, POST request! Hello, PUT request! Hello, DELETE request!
You can also use the @RequestMapping annotation to achieve the same functionality.
1package com.example.demo;23import org.springframework.web.bind.annotation.RequestMapping;4import org.springframework.web.bind.annotation.RequestMethod;5import org.springframework.web.bind.annotation.RestController;67@RestController8@RequestMapping("/api")9public class RequestMappingController {1011@RequestMapping(value = "/hello", method = RequestMethod.GET)12public String sayHello() {13return "Hello, GET request!";14}1516@RequestMapping(value = "/hello", method = RequestMethod.POST)17public String createHello() {18return "Hello, POST request!";19}2021@RequestMapping(value = "/hello", method = RequestMethod.PUT)22public String updateHello() {23return "Hello, PUT request!";24}2526@RequestMapping(value = "/hello", method = RequestMethod.DELETE)27public String deleteHello() {28return "Hello, DELETE request!";29}30}
You can also combine class-level and method-level mappings to create more complex URL structures.
1package com.example.demo;23import org.springframework.web.bind.annotation.GetMapping;4import org.springframework.web.bind.annotation.RequestMapping;5import org.springframework.web.bind.annotation.RestController;67@RestController8@RequestMapping("/api")9public class RequestMappingController {1011@GetMapping("/hello")12public String sayHello() {13return "Hello, GET request!";14}1516@RequestMapping(value = "/greet", method = RequestMethod.POST)17public String createGreeting() {18return "Greetings, POST request!";19}20}
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!