Spring Boot

Bean Validation

Bean Validation, annotations (@NotNull, @Size, @Email), @Valid, BindingResult, custom validators

25 preguntas de entrevista·
Mid-Level
1

What is the role of the @Valid annotation in Spring?

Respuesta

@Valid triggers Bean Validation constraints (JSR 303/380) on the annotated object. This annotation comes from the jakarta.validation package and is recognized by Spring to automatically validate DTOs in controllers. Use @Valid with BindingResult to retrieve errors without throwing an exception.

2

What is the main difference between @NotNull and @NotBlank for a String?

Respuesta

@NotNull only checks that the value is not null, while @NotBlank checks that the String is neither null, nor empty, nor composed only of whitespace. @NotBlank is stricter and suitable for text fields where spaces are not accepted. Use @NotBlank for required fields like username or email.

3

What does the @Size(min=3, max=50) annotation check?

Respuesta

@Size validates that the size (length for String, number of elements for collections) is between min and max inclusive. This constraint applies to String, Collection, Map and Array. For String validation, @Size checks the number of characters, not bytes.

4

Which annotation should be used to validate an email address?

5

Which constraint should be used to validate that a number is greater than or equal to 0?

+22 preguntas de entrevista

Domina Spring Boot para tu próxima entrevista

Accede a todas las preguntas, flashcards, tests técnicos, ejercicios de code review y simuladores de entrevista.

Empieza gratis