@PostMapping helps your Spring Boot app listen for POST requests easily. It connects a web address to a method that handles data sent by users.
@PostMapping for POST requests in Spring Boot
@PostMapping("/path")
public ReturnType methodName(@RequestBody DataType data) {
// handle POST request
}The string inside @PostMapping("/path") is the URL path this method listens to.
@RequestBody tells Spring to convert the incoming data (like JSON) into a Java object.
@PostMapping("/submit") public String submitData(@RequestBody String data) { return "Received: " + data; }
@PostMapping("/users") public User createUser(@RequestBody User user) { // save user to database return user; }
This Spring Boot controller listens for POST requests at /greet. It expects a plain text name in the request body and responds with a greeting message.
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { @PostMapping("/greet") public String greetUser(@RequestBody String name) { return "Hello, " + name + "!"; } }
Always use @RequestBody to get data from the POST request body.
Make sure your client sends data in the correct format (like JSON or plain text) matching your method parameter.
Use @RestController to make your class ready to handle web requests and return responses directly.
@PostMapping connects a URL path to a method that handles POST requests.
Use it when you want to receive and process data sent by users or clients.
Remember to use @RequestBody to get the data from the request body.
Practice
@PostMapping annotation in Spring Boot?Solution
Step 1: Understand HTTP methods
HTTP POST is used to send data to the server, so a method handling POST requests must be mapped accordingly.Step 2: Role of @PostMapping
@PostMapping specifically maps HTTP POST requests to a method in a Spring Boot controller.Final Answer:
To map HTTP POST requests to a specific method -> Option BQuick Check:
@PostMapping = POST request handler [OK]
- Confusing @PostMapping with @GetMapping
- Using @PostMapping for GET requests
- Not understanding HTTP method differences
Solution
Step 1: Identify correct annotation and parameter
The method must use @PostMapping to handle POST requests and @RequestBody to receive JSON data from the request body.Step 2: Check each option
@PostMapping("/submit") public String submitData(@RequestBody String data) { return "Received"; } uses @PostMapping and @RequestBody correctly. @GetMapping("/submit") public String submitData(@RequestBody String data) { return "Received"; } uses @GetMapping which is wrong. @PostMapping("/submit") public String submitData(String data) { return "Received"; } misses @RequestBody, so data won't be parsed from JSON. @PostMapping("/submit") public String submitData(@RequestParam String data) { return "Received"; } uses @RequestParam which reads query parameters, not JSON body.Final Answer:
@PostMapping("/submit") public String submitData(@RequestBody String data) { return "Received"; } -> Option CQuick Check:
@PostMapping + @RequestBody = correct syntax [OK]
- Using @GetMapping instead of @PostMapping
- Forgetting @RequestBody for JSON input
- Using @RequestParam for JSON body data
/greet?
@PostMapping("/greet")
public String greet(@RequestBody String name) {
return "Hi, " + name + "!";
}Solution
Step 1: Understand method behavior
The method takes the request body as a String parameter named 'name' and returns "Hi, " plus that name plus "!".Step 2: Apply input to output
Input body is "Hello", so the output will be "Hi, Hello!".Final Answer:
"Hi, Hello!" -> Option AQuick Check:
Input "Hello" returns "Hi, Hello!" [OK]
- Ignoring @RequestBody and expecting query param
- Confusing output with input
- Assuming endpoint is missing (404)
Solution
Step 1: Check parameter binding
The method expects to receive data in the POST request body, so the parameter must be annotated with @RequestBody to bind JSON or raw data.Step 2: Verify annotations
Without @RequestBody, Spring won't map the request body to the 'user' parameter, causing it to be null or empty.Final Answer:
Missing @RequestBody annotation for 'user' parameter -> Option DQuick Check:
POST body needs @RequestBody on parameter [OK]
- Forgetting @RequestBody on POST method parameters
- Using @GetMapping for POST endpoints
- Confusing @PathVariable with request body
/register that accepts a JSON object with fields username and email, maps it to a Java record UserData(String username, String email), and returns a confirmation message. Which method signature correctly implements this?Solution
Step 1: Understand data binding for POST JSON
To map JSON request body to a Java record, the parameter must be annotated with @RequestBody.Step 2: Check each option
@PostMapping("/register") public String registerUser(@RequestBody UserData user) { return "User " + user.username() + " registered with email " + user.email(); } correctly uses @RequestBody with UserData record and accesses its fields. @PostMapping("/register") public String registerUser(UserData user) { return "User " + user.username() + " registered"; } misses @RequestBody, so mapping fails. Options C and D use @RequestParam and @PathVariable which are for query/path parameters, not JSON body.Final Answer:
@PostMapping("/register") public String registerUser(@RequestBody UserData user) { return "User " + user.username() + " registered with email " + user.email(); } -> Option AQuick Check:
@RequestBody + record parameter = correct POST JSON mapping [OK]
- Omitting @RequestBody on complex objects
- Using @RequestParam or @PathVariable for JSON body
- Not accessing record fields correctly
Ready to prove it?
Pass the test and this pattern moves unknown
