Spring Boot Validation and Error Handling 1 — Questions and Answers
Question 1: Which annotation triggers Bean Validation on a method parameter in a Spring MVC controller?
- @Validate
- @Valid (Correct answer)
- @Constraint
- @NotNull
Correct answer: @Valid
@Valid (from javax/jakarta.validation) triggers Bean Validation cascading on the annotated parameter when used in controller method signatures.
Question 2: What is the key difference between @NotEmpty and @NotBlank in Bean Validation?
- @NotEmpty checks for null only; @NotBlank checks for whitespace
- @NotEmpty rejects null and empty strings; @NotBlank also rejects strings containing only whitespace (Correct answer)
- They are identical and interchangeable
- @NotBlank applies only to numeric fields; @NotEmpty applies to collections
Correct answer: @NotEmpty rejects null and empty strings; @NotBlank also rejects strings containing only whitespace
@NotEmpty rejects null and empty strings (""), while @NotBlank additionally rejects strings that consist solely of whitespace characters.
Question 3: When a controller method parameter annotated with @Valid fails validation, which exception is thrown by Spring MVC by default?
- ConstraintViolationException
- ValidationException
- MethodArgumentNotValidException (Correct answer)
- BindException
Correct answer: MethodArgumentNotValidException
Spring MVC throws MethodArgumentNotValidException when @Valid validation on a @RequestBody parameter fails, wrapping the BindingResult with all constraint violations.
Question 4: Which interface must be added alongside the BindingResult parameter in a controller method to capture validation errors without throwing an exception?
- The BindingResult parameter must immediately follow the @Valid parameter (Correct answer)
- BindingResult must be declared before the @Valid parameter
- BindingResult must be a separate method annotated with @ErrorHandler
- BindingResult is not supported in Spring Boot controllers
Correct answer: The BindingResult parameter must immediately follow the @Valid parameter
Spring MVC requires the BindingResult parameter to immediately follow the @Valid/@Validated parameter so that validation errors are captured rather than propagated as exceptions.
Question 5: What does the @ControllerAdvice annotation do in the context of exception handling?
- It marks a class as a Spring component that provides controller-level configuration
- It designates a class to handle exceptions globally across all controllers (Correct answer)
- It enables AOP advice on controller method return values
- It registers a filter that intercepts controller exceptions
Correct answer: It designates a class to handle exceptions globally across all controllers
@ControllerAdvice designates a class whose @ExceptionHandler, @InitBinder, and @ModelAttribute methods apply globally across all @Controller classes in the application.
Question 6: How do you create a custom constraint annotation in Bean Validation?
- Extend AbstractConstraint and override the validate() method
- Annotate the annotation with @Constraint(validatedBy=...) and implement ConstraintValidator (Correct answer)
- Implement Validator<T> and register it as a Spring bean
- Use @CustomValidator on the field and provide a validation class name
Correct answer: Annotate the annotation with @Constraint(validatedBy=...) and implement ConstraintValidator
A custom constraint requires an annotation meta-annotated with @Constraint(validatedBy=YourValidator.class) and a class implementing ConstraintValidator<YourAnnotation, TargetType>.
Question 7: Which annotation is used to define validation groups, and how are groups specified on a constraint?
- @GroupSequence on the constraint; groups are listed as an attribute
- groups={} attribute on the constraint annotation; activated via @Validated(Group.class) on the controller (Correct answer)
- Validation groups require a separate XML configuration file
- @Profile annotation is used to switch validation groups
Correct answer: groups={} attribute on the constraint annotation; activated via @Validated(Group.class) on the controller
Constraint annotations accept a groups={} attribute to assign them to a group interface, and @Validated(Group.class) on the controller method activates only that group's constraints.
Which annotation triggers Bean Validation on a method parameter in a Spring MVC controller?