Spring Boot

Bean Validation

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

25 Interview-Fragen·
Mid-Level
1

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

Antwort

@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?

Antwort

@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?

Antwort

@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 Interview-Fragen

Meistere Spring Boot für dein nächstes Interview

Zugang zu allen Fragen, Flashcards, technischen Tests, Code-Review-Übungen und Interview-Simulatoren.

Kostenlos starten