Sub topic 2
Jakarta Bean Validation and the validation starter
This chapter assumes you can build a Spring MVC controller, add a Maven or Gradle dependency, and read Java annotations and records. It explains which libraries do the validation and how Spring Boot wires them in. The chapters that follow use that setup.
Specification and implementation
Jakarta Bean Validation is a specification. It defines the constraint annotations, such as @NotBlank and @Min, and the jakarta.validation.Validator interface that checks an object against them. A specification contains no checking logic, so you need an implementation. Hibernate Validator is the reference implementation, and Spring Boot uses it by default.
| Piece | Role |
|---|---|
| `jakarta.validation-api` | Annotations and interfaces such as `Validator` |
| Hibernate Validator | Reads the annotations and runs the checks |
| Spring Boot auto-configuration | Creates a ready-to-use `Validator` bean |
| Spring MVC | Calls the validator when a parameter is marked for validation |
Spring Boot 3 and later use the jakarta.validation package. Boot 2 used javax.validation. If an old import won't compile, the package name is the likely cause.
Adding the starter
Since Spring Boot 2.3, spring-boot-starter-web no longer includes validation. You must add the starter yourself.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>The Gradle equivalent is:
implementation 'org.springframework.boot:spring-boot-starter-validation'The starter brings in the API, Hibernate Validator, and an Expression Language implementation. Hibernate Validator needs that implementation to interpolate messages.
The Validator bean
When the starter is on the classpath, Boot registers a LocalValidatorFactoryBean. It implements jakarta.validation.Validator, so you can inject it anywhere. Spring MVC uses the same bean when you mark a controller parameter for validation, which a later chapter covers.
The example below validates an object without any web layer. It shows that Bean Validation is plain Java and works on any object.
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.NotBlank;
import java.util.Set;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public record Greeting(@NotBlank String text) {
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner check(Validator validator) {
return args -> {
Set<ConstraintViolation<Greeting>> violations =
validator.validate(new Greeting(" "));
violations.forEach(v ->
System.out.println(v.getPropertyPath() + ": " + v.getMessage()));
};
}
}On startup this prints text: must not be blank. Each ConstraintViolation records the property path, the message, and the rejected value. Spring MVC later converts these violations into its own error types.
Common errors
- Annotations are ignored. The starter is missing. Without an implementation,
@NotBlankcompiles but nothing checks it. Boot 2.3 and later won't warn you. NoClassDefFoundErrororHV000183about Expression Language. You added onlyhibernate-validatorby hand. Use the starter, or add an EL implementation yourself.javax.validationimports fail. You are on Boot 3 or later. Change them tojakarta.validation.- Wrong
Validatortype.org.springframework.validation.Validatoris Spring's own interface. Importjakarta.validation.Validatorfor the API shown here.
Exercise
You add @NotBlank to a DTO field in a Boot 3 project that has only spring-boot-starter-web. Blank values are accepted. What is the likely cause and the fix?
Show answer
No Bean Validation implementation is on the classpath, because spring-boot-starter-web doesn't include one. Add spring-boot-starter-validation, which brings in Hibernate Validator and the auto-configured Validator bean. The field is also only checked once validation is triggered, which later chapters cover.