Sub topic 8
Failing fast with validated properties
This chapter assumes you can already bind properties to a @ConfigurationProperties class, as covered in the previous chapter. It also assumes you know how profiles override values.
Why validate at startup
A wrong setting is cheapest to find when the application starts. Without validation, a blank host or a port of 0 can sit unnoticed until the first email is sent, possibly in production at 3 a.m. With validation, Spring Boot refuses to start and tells you which property is wrong.
Adding validation
Validation uses Jakarta Bean Validation. Add the validation starter to your build, because Boot does not include a validator by default.
Then annotate the properties class with @Validated and put constraints on its components.
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.validation.annotation.Validated;
@Validated
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
@NotBlank String host,
@Min(1) @Max(65535) int port,
@Email String sender,
@DefaultValue("3") int retryAttempts) {
}The constraints say that host must contain text, port must be a valid TCP port, and sender must look like an email address. @DefaultValue("3") supplies a value when app.mail.retry-attempts is missing. Relaxed binding maps the kebab-case key retry-attempts to the component retryAttempts.
What happens at startup
Binding runs first, then validation. A failure in either stops the application.
Suppose you launch with an invalid port:
java -jar app.jar --app.mail.port=70000Boot prints a failure report that names the property and the broken constraint. It looks similar to this:
APPLICATION FAILED TO START
Description:
Binding to target ... MailProperties failed:
Property: app.mail.port
Value: "70000"
Reason: must be less than or equal to 65535The report names the property, the value, and the reason, so you can fix the setting without reading a stack trace. The same check applies to values that arrive from profile files, environment variables, or the command line, because validation runs after all sources are merged.
Common errors
- Constraints are ignored. The validation starter is missing, or
@Validatedis absent from the class. - Nested objects are not checked. Constraints inside a nested record or class run only if the nested component is annotated with
@Valid. - A missing value passes.
@Min(1)on a primitiveintsees0when the property is absent, so it fails.@Emailon aStringacceptsnull. Add@NotBlankwhen a value is required. - The property fails in one profile only. The invalid value likely comes from a profile override, such as the prod document replacing
host.
Exercise
You add @Min(1) @Max(65535) to a port component, but starting the application with --app.mail.port=70000 still succeeds. Name two likely causes.
Show answer
Either spring-boot-starter-validation is not on the classpath, so no validator runs, or the class is missing @Validated. Both leave the constraints unchecked.