Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to accept JSON input in a Spring Boot controller method.
Spring Boot
public ResponseEntity<String> createUser([1] User user) {
// method body
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @PathVariable instead of @RequestBody
Forgetting to annotate the parameter
✗ Incorrect
The @RequestBody annotation tells Spring Boot to convert the JSON input into a User object.
2fill in blank
mediumComplete the code to specify the HTTP method and path for JSON input handling.
Spring Boot
@[1]("/users") public ResponseEntity<String> addUser(@RequestBody User user) { // method body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetMapping which does not support request body
Using DeleteMapping or PutMapping incorrectly
✗ Incorrect
PostMapping is used to handle HTTP POST requests, which are commonly used to send JSON data to create resources.
3fill in blank
hardFix the error in the method parameter to correctly receive JSON input.
Spring Boot
public ResponseEntity<String> updateUser([1] user) {
// method body
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Missing @RequestBody annotation
Using @RequestParam or @PathVariable incorrectly
✗ Incorrect
The parameter must be annotated with @RequestBody to tell Spring to convert JSON input to the User object.
4fill in blank
hardFill both blanks to create a controller method that accepts JSON and returns a response.
Spring Boot
@[1]("/products") public ResponseEntity<[2]> addProduct(@RequestBody Product product) { // method body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetMapping for JSON input
Returning the Product object instead of a response message
✗ Incorrect
PostMapping handles POST requests for JSON input. The method returns a ResponseEntity to send a response message.
5fill in blank
hardFill all three blanks to define a Spring Boot controller method that accepts JSON, validates it, and returns a response.
Spring Boot
@[1]("/orders") public ResponseEntity<[2]> placeOrder(@Valid @RequestBody [3] order) { // method body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GetMapping instead of PostMapping
Missing @RequestBody or @Valid annotations
Wrong return type
✗ Incorrect
PostMapping is for POST requests. The method returns ResponseEntity. The parameter is an Order object annotated with @RequestBody and @Valid for validation.