Sub topic 3
Declaring constraints on a request DTO
This chapter assumes you can write a Spring MVC controller, read Java records and annotations, and add a dependency in Maven or Gradle. You will declare validation rules on a request DTO (data transfer object, the class that carries the request body). Triggering those rules comes in the next chapter.
Adding the starter
Spring Boot does not include validation by default. Add the validation starter, which brings in Jakarta Bean Validation and Hibernate Validator, its reference implementation.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>Boot auto-configures a Validator bean once this is on the classpath.
Constraints as annotations
A constraint is an annotation that states a rule about a value. You put it on a field, or on a record component. Bean Validation reads these annotations at runtime and checks the value against each rule.
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
public record CreateUserRequest(
@NotBlank @Size(max = 50) String name,
@NotBlank @Email String email,
@NotNull @Min(18) Integer age) {
}Each component carries its own rules. A value must satisfy every annotation on it. The annotations are declarative: the record holds no validation code.
The standard constraints you will use most
| Annotation | Applies to | Rule |
|---|---|---|
| `@NotNull` | any type | value is not `null` |
| `@NotEmpty` | strings, collections, maps, arrays | not `null` and size above 0 |
| `@NotBlank` | strings | not `null` and contains a non-whitespace character |
| `@Size(min, max)` | strings, collections, maps, arrays | length or size within the bounds |
| `@Min`, `@Max` | numbers | value within the bound |
| `@Positive` | numbers | value above 0 |
| `@Email` | strings | looks like an email address |
| `@Pattern(regexp)` | strings | matches the regular expression |
Null handling
Most constraints treat null as valid. @Min(18) on an Integer accepts null, and so does @Size(max = 50) and @Email. The rule is that a constraint checks a value only when there is one, and presence is a separate rule.
That is why age above has both @NotNull and @Min(18). Without @NotNull, a JSON body that omits age would pass.
The exceptions are @NotNull, @NotEmpty and @NotBlank, which exist to reject null.
Overriding the message
Every constraint has a default message. Set a custom one with the message attribute.
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public record RenameRequest(
@NotBlank(message = "name is required")
@Size(max = 50, message = "name must be at most {max} characters")
String name,
@Min(value = 18, message = "must be at least {value}") int minimumAge) {
}Placeholders in braces, such as {max}, are filled from the annotation's attributes. A later chapter covers moving messages into a properties file.
How the rules fit together
The next diagram shows how one value is checked against the annotations on a component.
In practice Hibernate Validator runs all constraints on a component and collects every violation. It does not stop at the first one, and the order between annotations on the same component is not guaranteed.
Common mistakes
- Using
@NotNullon a string and expecting it to reject"". Use@NotBlank. - Using
@Size(min = 1)and expecting it to rejectnull. It acceptsnull. - Importing
javax.validationannotations. Spring Boot 3 and later usejakarta.validation. - Expecting the annotations to do anything by themselves. They are only metadata until something calls the validator.
Exercise
A request record has @Min(1) Integer quantity. A client sends {} as the body. Does validation fail on quantity, and what do you change if you want it to fail?
Show answer
It does not fail. quantity is null, and @Min treats null as valid. Add @NotNull so the component reads @NotNull @Min(1) Integer quantity.
Exercise
Which annotation rejects the string " " (only spaces): @NotNull, @NotEmpty or @NotBlank?
Show answer
@NotBlank. @NotNull and @NotEmpty both accept a string that contains only spaces.