Spring Boot

Bean Validation

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

25 面接問題·
Mid-Level
1

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

回答

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

回答

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

回答

@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 面接問題

次の面接に向けてSpring Bootをマスター

すべての問題、flashcards、技術テスト、コードレビュー演習、面接シミュレーターにアクセス。

無料で始める