Challenge - 5 Problems
Spring Boot Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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");
}Attempts:
2 left
💡 Hint
Think about how Spring Boot handles validation exceptions by default.
✗ Incorrect
When @Valid fails, Spring Boot automatically returns a 400 Bad Request response with details about the validation errors.
📝 Syntax
intermediate2: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.
Attempts:
2 left
💡 Hint
Remember that @Valid must be combined with @RequestBody to validate JSON input.
✗ Incorrect
@Valid must be placed on the parameter annotated with @RequestBody to trigger validation of the incoming JSON.
🔧 Debug
advanced2: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");
}Attempts:
2 left
💡 Hint
Check if the parameter is annotated with @Valid.
✗ Incorrect
Validation only triggers if @Valid is present on the @RequestBody parameter.
❓ state_output
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how Spring Boot formats validation error responses.
✗ Incorrect
Spring Boot returns a JSON response with details about each field that failed validation.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about global exception handling in Spring Boot.
✗ Incorrect
Using @ControllerAdvice with @ExceptionHandler for MethodArgumentNotValidException lets you customize validation error responses.