0
0
Spring Bootframework~20 mins

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

Choose your learning style9 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.