0
0
Spring Bootframework~20 mins

Request validation preview in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Spring Boot controller receives invalid input with @Valid?

Consider a Spring Boot REST controller method annotated with @Valid on a request body. What is the default behavior when the input fails validation?

Spring Boot
public record UserRequest(@NotBlank String name, @Min(18) int age) {}

@PostMapping("/users")
public ResponseEntity<String> createUser(@Valid @RequestBody UserRequest user) {
    return ResponseEntity.ok("User created");
}
ASpring Boot automatically returns a 400 Bad Request with a validation error message without entering the controller method.
BThe controller method runs and returns a 200 OK with "User created" regardless of validation errors.
CThe controller method throws a NullPointerException if validation fails.
DThe controller method returns a 500 Internal Server Error when validation fails.
Attempts:
2 left
💡 Hint

Think about how Spring Boot handles validation exceptions by default.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly applies validation annotations to a Spring Boot request DTO?

Choose the code snippet that correctly uses validation annotations on a Spring Boot record used as a request DTO.

Apublic record ProductRequest(@NotNull String name, @Size(min=1) int quantity) {}
Bpublic record ProductRequest(@NotBlank int name, @Min(1) String quantity) {}
Cpublic record ProductRequest(@Min(1) String name, @NotEmpty int quantity) {}
Dpublic record ProductRequest(@NotEmpty String name, @Positive int quantity) {}
Attempts:
2 left
💡 Hint

Remember which annotations apply to which data types.

🔧 Debug
advanced
2:00remaining
Why does this Spring Boot controller not return validation errors as expected?

Given the controller method below, why might validation errors not trigger a 400 response?

Spring Boot
public record OrderRequest(@NotNull String productId, @Min(1) int quantity) {}

@PostMapping("/orders")
public ResponseEntity<String> placeOrder(OrderRequest order) {
    return ResponseEntity.ok("Order placed");
}
AThe <code>@Min</code> annotation cannot be used on primitive int types.
BThe <code>OrderRequest</code> record must be a class, not a record, for validation to work.
CThe <code>@RequestBody</code> annotation is missing, so Spring does not bind the JSON to the object and skip validation.
DThe <code>@Valid</code> annotation is missing on the <code>OrderRequest</code> parameter, so validation is not triggered.
Attempts:
2 left
💡 Hint

Think about how Spring binds HTTP request data to method parameters.

state_output
advanced
2:00remaining
What is the HTTP response status and body when validation fails in this Spring Boot controller?

Given the controller method below, what is the HTTP status code and response body when the client sends {"name":"", "age":15}?

Spring Boot
public record UserRequest(@NotBlank String name, @Min(18) int age) {}

@PostMapping("/users")
public ResponseEntity<String> createUser(@Valid @RequestBody UserRequest user) {
    return ResponseEntity.ok("User created");
}
AStatus 400 Bad Request with a JSON body listing 'name' must not be blank and 'age' must be at least 18.
BStatus 200 OK with body 'User created'.
CStatus 500 Internal Server Error with empty body.
DStatus 422 Unprocessable Entity with plain text error message.
Attempts:
2 left
💡 Hint

Recall the default error response Spring Boot sends on validation failure.

🧠 Conceptual
expert
3:00remaining
How can you customize the validation error response in Spring Boot globally?

Which approach allows you to globally customize the JSON structure returned when validation errors occur in a Spring Boot REST API?

AAdd a custom message attribute to each validation annotation on the DTO fields.
BImplement a @ControllerAdvice class with an @ExceptionHandler for MethodArgumentNotValidException to build a custom response body.
COverride the toString() method of the request DTO to return custom error messages.
DUse @ResponseStatus on the controller method to change the HTTP status code.
Attempts:
2 left
💡 Hint

Think about how Spring Boot handles exceptions globally.