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, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기