0
0
Spring Bootframework~20 mins

@Valid annotation on request body in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when @Valid fails on a request body?
Consider a Spring Boot controller method that uses @Valid on a request body object. What is the default behavior when the validation fails?
Spring Boot
public ResponseEntity<String> createUser(@Valid @RequestBody User user) {
    return ResponseEntity.ok("User created");
}
AThe method returns HTTP 400 Bad Request with validation error details.
BThe method proceeds normally and returns HTTP 200 OK.
CThe method throws a NullPointerException.
DThe method returns HTTP 500 Internal Server Error.
Attempts:
2 left
💡 Hint
Think about how Spring Boot handles validation exceptions by default.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly applies @Valid on a request body?
Select the code snippet that correctly uses @Valid to validate a request body in a Spring Boot controller.
Apublic ResponseEntity<String> add(@RequestBody User user) { return ResponseEntity.ok("Added"); }
Bpublic ResponseEntity<String> add(@RequestBody @Valid User user) { return ResponseEntity.ok("Added"); }
Cpublic ResponseEntity<String> add(@Valid User user) { return ResponseEntity.ok("Added"); }
Dpublic ResponseEntity<String> add(User user) { return ResponseEntity.ok("Added"); }
Attempts:
2 left
💡 Hint
Remember that @Valid must be combined with @RequestBody to validate JSON input.
🔧 Debug
advanced
2:00remaining
Why does validation not trigger with @Valid on a request body?
Given this controller method, validation does not trigger even though @Valid is present. What is the most likely cause? public ResponseEntity updateUser(@RequestBody User user) { return ResponseEntity.ok("Updated"); }
Spring Boot
public ResponseEntity<String> updateUser(@RequestBody User user) {
    return ResponseEntity.ok("Updated");
}
AThe method return type must be void for validation to work.
BThe User class does not have any validation annotations.
CThe controller method is missing @PostMapping annotation.
DThe @Valid annotation is missing on the parameter.
Attempts:
2 left
💡 Hint
Check if the parameter is annotated with @Valid.
state_output
advanced
2:00remaining
What is the response body when validation fails on a @Valid request body?
If a POST request sends invalid data to a Spring Boot controller method with @Valid on the request body, what does the response body contain by default?
AA JSON object listing field errors and messages.
BAn empty response body with status 400.
CA plain text message 'Validation failed'.
DThe original invalid JSON sent by the client.
Attempts:
2 left
💡 Hint
Think about how Spring Boot formats validation error responses.
🧠 Conceptual
expert
3:00remaining
How to customize validation error responses for @Valid request bodies?
Which approach allows customizing the error response sent when @Valid validation fails on a request body in Spring Boot?
AAdd a @ResponseStatus annotation on the controller method.
BOverride the toString() method of the request body class.
CImplement a @ControllerAdvice class with an @ExceptionHandler for MethodArgumentNotValidException.
DUse @JsonIgnore on all fields in the request body class.
Attempts:
2 left
💡 Hint
Think about global exception handling in Spring Boot.