0
0
Spring Bootframework~20 mins

@RequestBody for JSON input in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of @RequestBody JSON Input
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when sending JSON to a Spring Boot controller with @RequestBody?
Consider this Spring Boot controller method:
@PostMapping("/user")
public String addUser(@RequestBody User user) {
    return user.getName();
}

If you send this JSON:
{"name":"Alice","age":30}

What will be the response?
Spring Boot
@PostMapping("/user")
public String addUser(@RequestBody User user) {
    return user.getName();
}
Anull
B"{\"name\":\"Alice\",\"age\":30}"
CHTTP 400 Bad Request error
D"Alice"
Attempts:
2 left
💡 Hint
Think about how @RequestBody converts JSON into a Java object.
📝 Syntax
intermediate
2:00remaining
Which method signature correctly uses @RequestBody to accept JSON input?
You want to write a Spring Boot controller method that accepts JSON data for a Product object. Which of these method signatures is correct?
Apublic ResponseEntity<String> addProduct(Product product)
Bpublic ResponseEntity<String> addProduct(@PathVariable Product product)
Cpublic ResponseEntity<String> addProduct(@RequestBody Product product)
Dpublic ResponseEntity<String> addProduct(@RequestParam Product product)
Attempts:
2 left
💡 Hint
Remember that @RequestBody is needed to convert JSON in the request body to a Java object.
🔧 Debug
advanced
2:00remaining
Why does this Spring Boot controller method fail to parse JSON with @RequestBody?
Given this method:
@PostMapping("/order")
public String createOrder(@RequestBody String order) {
    return order;
}

When sending JSON, the client gets a 415 Unsupported Media Type error. What is the likely cause?
Spring Boot
@PostMapping("/order")
public String createOrder(@RequestBody String order) {
    return order;
}
AThe client did not set Content-Type to application/json
BThe method parameter should be an Order object, not String
CThe method must use @RequestParam instead of @RequestBody
DSpring Boot does not support JSON input by default
Attempts:
2 left
💡 Hint
@RequestBody String does not support application/json; use a POJO instead.
state_output
advanced
2:00remaining
What is the value of the User object after JSON input with missing fields?
Given this User class:
public class User {
  private String name;
  private int age;
  // getters and setters
}

And this controller method:
@PostMapping("/user")
public String addUser(@RequestBody User user) {
    return user.getName() + "," + user.getAge();
}

If the JSON sent is:
{"name":"Bob"}

What will be the returned string?
Spring Boot
@PostMapping("/user")
public String addUser(@RequestBody User user) {
    return user.getName() + "," + user.getAge();
}
A"Bob,0"
BHTTP 400 Bad Request error
C"null,0"
D"Bob,null"
Attempts:
2 left
💡 Hint
What is the default value for an int field in Java if not set?
🧠 Conceptual
expert
2:00remaining
Which statement about @RequestBody and JSON input is true?
Select the correct statement about how Spring Boot handles JSON input with @RequestBody.
A@RequestBody automatically validates JSON fields and throws errors if any field is missing
B@RequestBody requires a default constructor and setters or a constructor with parameters to map JSON fields
C@RequestBody can only map JSON arrays, not JSON objects
D@RequestBody ignores Content-Type headers and accepts any media type
Attempts:
2 left
💡 Hint
Think about how Jackson or other JSON mappers create Java objects.