Bean Validation
Bean Validation, annotations (@NotNull, @Size, @Email), @Valid, BindingResult, custom validators
1What is the role of the @Valid annotation in Spring?
What is the role of the @Valid annotation in Spring?
Answer
@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.
2What is the main difference between @NotNull and @NotBlank for a String?
What is the main difference between @NotNull and @NotBlank for a String?
Answer
@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.
3What does the @Size(min=3, max=50) annotation check?
What does the @Size(min=3, max=50) annotation check?
Answer
@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.
Which annotation should be used to validate an email address?
Which constraint should be used to validate that a number is greater than or equal to 0?
+22 interview questions
Other Spring Boot interview topics
Spring Core - IoC & DI
Spring Boot Auto-Configuration
Spring Boot Starters
Application Properties & YAML
Logging with SLF4J & Logback
Spring Boot DevTools
Spring MVC Basics
Spring REST Controllers
Request & Response Handling
Exception Handling
Spring Data JPA Basics
JPA Entities & Relationships
JPA Queries
Spring Data Repositories
Spring Security Basics
Spring Boot Actuator
Unit Testing with JUnit & Mockito
Spring Boot Testing
Profiles & Environment
RestTemplate & WebClient
Async & Scheduling
Caching with Spring
Spring WebFlux (Reactive)
Spring Transactions
Advanced Authentication & Authorization
JWT & Stateless Security
OAuth2 & Authorization Server
Spring Boot & Docker
Microservices with Spring
Spring Cloud Config
Performance Optimization
GraalVM Native Images
Master Spring Boot for your next interview
Access all questions, flashcards, technical tests, code review exercises and interview simulators.
Start for free