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"}?
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() + "!"; } }
Look at the return type and how the method uses the request body.
The method returns a plain string greeting using the name from the JSON request body. Spring Boot sends this string as the HTTP response 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.
Remember how to bind JSON request bodies to Java objects.
Option B correctly uses @RequestBody to map the JSON body to the User object and specifies the path /submit.
Given this Spring Boot controller method, why does sending a POST request with JSON {"username":"bob"} cause a 400 Bad Request error?
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(); } }
Check if the JSON keys match the Java record components.
The JSON key "username" does not match the record component "name", so Spring cannot map the JSON to the record, causing a 400 error.
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?
import org.springframework.web.bind.annotation.*; @RestController public class CounterController { private int count = 0; @PostMapping("/count") public int increment() { count++; return count; } }
Think about how instance variables behave in Spring controllers.
The controller instance keeps the count variable, so after two POST requests, it increments twice to 2.
Choose the correct statement about how Spring Boot handles methods annotated with @PostMapping.
Think about how Spring Boot handles JSON and Java object mapping.
Spring Boot uses message converters to automatically convert JSON request bodies to Java objects when @RequestBody is used and content type is application/json.