Spring Boot

Bean Validation

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

25 câu hỏi phỏng vấn·
Mid-Level
1

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

Câu trả lời

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

Câu trả lời

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

Câu trả lời

@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 câu hỏi phỏng vấn

Nắm vững Spring Boot cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí