Bird
Raised Fist0
Spring Bootframework~20 mins

@PostMapping for POST requests in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PostMapping Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot controller method?

Consider this Spring Boot controller method annotated with @PostMapping. What will be the HTTP response body when a POST request is sent to /greet with JSON {"name":"Alice"}?

Spring Boot
import org.springframework.web.bind.annotation.*;

@RestController
public class GreetingController {

    record NameRequest(String name) {}

    @PostMapping("/greet")
    public String greet(@RequestBody NameRequest request) {
        return "Hello, " + request.name() + "!";
    }
}
A"{\"message\":\"Hello, Alice!\"}"
BHTTP 500 Internal Server Error
CHTTP 404 Not Found
D"Hello, Alice!"
Attempts:
2 left
💡 Hint

Look at the return type and how the method uses the request body.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses @PostMapping to handle POST requests with a JSON body?

Choose the correct method signature for a Spring Boot controller method that handles POST requests to /submit and accepts a JSON body mapped to a User object.

A@PostMapping("/submit") public String submit(User user) { return user.getName(); }
B@PostMapping("/submit") public String submit(@RequestBody User user) { return user.getName(); }
C@PostMapping public String submit(@RequestBody User user) { return user.getName(); }
D@PostMapping("/submit") public String submit(@RequestParam User user) { return user.getName(); }
Attempts:
2 left
💡 Hint

Remember how to bind JSON request bodies to Java objects.

🔧 Debug
advanced
2:30remaining
Why does this @PostMapping method cause a 400 Bad Request error?

Given this Spring Boot controller method, why does sending a POST request with JSON {"username":"bob"} cause a 400 Bad Request error?

Spring Boot
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    record User(String name) {}

    @PostMapping("/user")
    public String createUser(@RequestBody User user) {
        return "User: " + user.name();
    }
}
AThe @PostMapping annotation requires a consumes attribute to accept JSON.
BThe method is missing @ResponseBody annotation.
CThe JSON field name "username" does not match the record component "name", causing deserialization failure.
DThe record User cannot be used as a request body parameter.
Attempts:
2 left
💡 Hint

Check if the JSON keys match the Java record components.

❓ state_output
advanced
2:00remaining
What is the value of 'count' after these POST requests?

Consider this Spring Boot controller with a counter incremented on each POST request to /count. What is the value of count after two POST requests?

Spring Boot
import org.springframework.web.bind.annotation.*;

@RestController
public class CounterController {

    private int count = 0;

    @PostMapping("/count")
    public int increment() {
        count++;
        return count;
    }
}
A2
B1
C0
DThrows an error due to concurrency
Attempts:
2 left
💡 Hint

Think about how instance variables behave in Spring controllers.

🧠 Conceptual
expert
2:30remaining
Which statement about @PostMapping and request handling is true?

Choose the correct statement about how Spring Boot handles methods annotated with @PostMapping.

ASpring Boot automatically converts JSON request bodies to Java objects if the method parameter is annotated with @RequestBody and the content type is application/json.
BMethods annotated with @PostMapping can only accept JSON request bodies and no other content types.
C@PostMapping methods must always return ResponseEntity objects to send responses.
D@PostMapping annotation disables HTTP GET requests for the same path automatically.
Attempts:
2 left
💡 Hint

Think about how Spring Boot handles JSON and Java object mapping.

Practice

(1/5)
1. What is the main purpose of the @PostMapping annotation in Spring Boot?
easy
A. To map HTTP PUT requests to a specific method
B. To map HTTP POST requests to a specific method
C. To map HTTP DELETE requests to a specific method
D. To map HTTP GET requests to a specific method

Solution

  1. 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.
  2. Step 2: Role of @PostMapping

    @PostMapping specifically maps HTTP POST requests to a method in a Spring Boot controller.
  3. Final Answer:

    To map HTTP POST requests to a specific method -> Option B
  4. Quick Check:

    @PostMapping = POST request handler [OK]
Hint: POST requests use @PostMapping to connect URL and method [OK]
Common Mistakes:
  • Confusing @PostMapping with @GetMapping
  • Using @PostMapping for GET requests
  • Not understanding HTTP method differences
2. Which of the following is the correct syntax to create a POST endpoint in Spring Boot that accepts JSON data?
easy
A. @PostMapping("/submit") public String submitData(String data) { return "Received"; }
B. @GetMapping("/submit") public String submitData(@RequestBody String data) { return "Received"; }
C. @PostMapping("/submit") public String submitData(@RequestBody String data) { return "Received"; }
D. @PostMapping("/submit") public String submitData(@RequestParam String data) { return "Received"; }

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    @PostMapping("/submit") public String submitData(@RequestBody String data) { return "Received"; } -> Option C
  4. Quick Check:

    @PostMapping + @RequestBody = correct syntax [OK]
Hint: Use @RequestBody to get JSON data in POST method [OK]
Common Mistakes:
  • Using @GetMapping instead of @PostMapping
  • Forgetting @RequestBody for JSON input
  • Using @RequestParam for JSON body data
3. Given the following Spring Boot controller method, what will be the output when a POST request with body "Hello" is sent to /greet? @PostMapping("/greet") public String greet(@RequestBody String name) { return "Hi, " + name + "!"; }
medium
A. "Hi, Hello!"
B. "Hello"
C. "Hi, name!"
D. HTTP 404 Not Found

Solution

  1. Step 1: Understand method behavior

    The method takes the request body as a String parameter named 'name' and returns "Hi, " plus that name plus "!".
  2. Step 2: Apply input to output

    Input body is "Hello", so the output will be "Hi, Hello!".
  3. Final Answer:

    "Hi, Hello!" -> Option A
  4. Quick Check:

    Input "Hello" returns "Hi, Hello!" [OK]
Hint: Return greeting by concatenating input string [OK]
Common Mistakes:
  • Ignoring @RequestBody and expecting query param
  • Confusing output with input
  • Assuming endpoint is missing (404)
4. Identify the error in this Spring Boot POST method: @PostMapping("/addUser") public String addUser(String user) { return "User " + user + " added"; }
medium
A. Method return type should be void
B. Incorrect HTTP method annotation, should be @GetMapping
C. Missing @PathVariable annotation
D. Missing @RequestBody annotation for 'user' parameter

Solution

  1. 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.
  2. Step 2: Verify annotations

    Without @RequestBody, Spring won't map the request body to the 'user' parameter, causing it to be null or empty.
  3. Final Answer:

    Missing @RequestBody annotation for 'user' parameter -> Option D
  4. Quick Check:

    POST body needs @RequestBody on parameter [OK]
Hint: Add @RequestBody to method parameter for POST data [OK]
Common Mistakes:
  • Forgetting @RequestBody on POST method parameters
  • Using @GetMapping for POST endpoints
  • Confusing @PathVariable with request body
5. You want to create a Spring Boot POST endpoint /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?
hard
A. @PostMapping("/register") public String registerUser(@RequestBody UserData user) { return "User " + user.username() + " registered with email " + user.email(); }
B. @PostMapping("/register") public String registerUser(UserData user) { return "User " + user.username() + " registered"; }
C. @PostMapping("/register") public String registerUser(@RequestParam UserData user) { return "User " + user.username() + " registered"; }
D. @PostMapping("/register") public String registerUser(@PathVariable UserData user) { return "User " + user.username() + " registered"; }

Solution

  1. Step 1: Understand data binding for POST JSON

    To map JSON request body to a Java record, the parameter must be annotated with @RequestBody.
  2. 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.
  3. Final Answer:

    @PostMapping("/register") public String registerUser(@RequestBody UserData user) { return "User " + user.username() + " registered with email " + user.email(); } -> Option A
  4. Quick Check:

    @RequestBody + record parameter = correct POST JSON mapping [OK]
Hint: Use @RequestBody with record to map JSON POST data [OK]
Common Mistakes:
  • Omitting @RequestBody on complex objects
  • Using @RequestParam or @PathVariable for JSON body
  • Not accessing record fields correctly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard