![]() |
VOOZH | about |
In Spring Boot, @GetMapping and @PostMapping are annotations used to handle HTTP requests in Spring MVC controllers. They help map specific HTTP methods to controller methods when building REST APIs.
@PostMapping in Spring MVC is used to handle HTTP POST requests in RESTful web services. It maps a specific URL to a handler method that processes data sent from the client, typically through the request body.
Example 1:
Explanation: the addStudent method is annotated with @PostMapping. It handles HTTP POST requests sent to the /addStudent URL. The student name is passed in the request body and processed by the method.
Example 2:
Explanation: the updateStudent method is annotated with @PostMapping. It handles POST requests sent to the /students/{id} URL. The student ID is received as a path variable, and the updated student object is passed in the request body.
@GetMapping in Spring is used to handle HTTP GET requests in RESTful web services. It maps a specific URL to a controller method that retrieves data from the server.
Example 1:
Explanation: getAllStudents method is annotated with @GetMapping annotation. This means that getAllStudents method will be called when an HTTP Get request is received at the /students URL.
Example 2:
Explanation: the getStudent method is annotated with @GetMapping. It handles HTTP GET requests sent to the /students/{rollNo} URL. The roll number is passed as a path variable to fetch the student details.
| Feature | @GetMapping | @PostMapping |
|---|---|---|
| HTTP Method | Handles GET requests | Handles POST requests |
| Purpose | Used to retrieve data from the server | Used to send data to the server (create/update) |
| Data Location | Data is passed through URL parameters / query strings | Data is sent in the request body |
| Effect on Server | Does not modify server data | May modify or create server data |
| Common Use Case | Fetching records, loading pages, reading APIs | Form submissions, creating resources in APIs |