Introduction
Request mapping connects web addresses and HTTP methods to specific code that runs when someone visits those addresses.
Request mapping connects web addresses and HTTP methods to specific code that runs when someone visits those addresses.
@RequestMapping(path = "/example", method = RequestMethod.GET)
public String exampleMethod() {
// code here
}@RequestMapping on methods or classes to link URLs and HTTP methods.@RequestMapping(path = "/hello", method = RequestMethod.GET) public String sayHello() { return "Hello!"; }
@RequestMapping(path = "/submit", method = RequestMethod.POST) public String submitData() { return "Data submitted!"; }
@RequestMapping(path = "/items", method = {RequestMethod.GET, RequestMethod.POST}) public String handleItems() { return "Items handled!"; }
This controller has two methods for the same path '/greet'. One runs on GET requests and says hello. The other runs on POST requests and thanks the user.
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(path = "/greet", method = RequestMethod.GET) public String greet() { return "Hello, visitor!"; } @RequestMapping(path = "/greet", method = RequestMethod.POST) public String greetPost() { return "Thanks for posting!"; } }
Spring Boot also offers shortcuts like @GetMapping and @PostMapping for simpler code.
Always match the HTTP method to what your code should do (GET for reading, POST for creating, etc.).
Request mapping links URLs and HTTP methods to code.
You can specify path and method with @RequestMapping.
This helps organize web app behavior clearly.
@RequestMapping annotation do in Spring Boot?@RequestMapping?@RequestMapping(path = "/test", method = RequestMethod.POST)
public String test() {
return "Success";
}
What happens when a client sends a GET request to "/test"?@RequestMapping(path = "/data", method = "POST")
public String saveData() {
return "Saved";
}Pass the test and this pattern moves unknown