Enabling Startup Parameter Validation in Spring Boot Using Java Validation
This article explains how to use Spring's Java Validation to check configuration properties at application startup, demonstrates the required annotations, lists supported validation constraints, and shows how to implement and register custom validators to enforce business rules early in the boot process.
When a Spring Boot project relies on configuration parameters, missing values often cause runtime exceptions only when a component is used. By enabling Java Validation on configuration properties, you can validate these parameters during application startup and fail fast if they are absent or incorrect.
To activate startup validation, annotate your @ConfigurationProperties class with @Validated (along with Lombok's @Data and Spring's @Component ) and add constraint annotations such as @NotEmpty(message="Configuration must contain [app.id]") to the fields that need validation:
@Validated
@Data
@ConfigurationProperties(prefix = "app")
@Component
public class AppConfigProperties {
@NotEmpty(message = "配置文件配置必须要配置[app.id]属性")
private String id;
}This configuration causes Spring to verify that application.yml contains the app.id entry; if it is missing, the application fails to start with a validation exception.
Note that the validation works only with @ConfigurationProperties ; the @Value annotation does not support this feature.
Inject the validated properties wherever needed:
@Autowired
private AppConfigProperties appConfigProperties;The framework supports a wide range of Bean Validation constraints, for example:
Constraint
Description
@Null
Value must be null
@NotNull
Value must not be null
@AssertFalse
Value must be false
@AssertTrue
Value must be true
@DecimalMax(value)
Numeric value must be less than or equal to the specified maximum
@DecimalMin(value)
Numeric value must be greater than or equal to the specified minimum
@Digits(integer,fraction)
Numeric value must have at most
integerdigits in the integer part and
fractiondigits in the fractional part
@Future
Value must be a future date
@Max(value)
Numeric value must be less than or equal to the specified maximum
@Min(value)
Numeric value must be greater than or equal to the specified minimum
@Past
Value must be a past date
@Pattern(value)
Value must match the given regular expression
@Size(min,max)
String or collection size must be between
minand
max@NotEmpty
Value must not be null and must contain at least one element (for strings, length > 0)
@NotBlank
String must contain at least one non‑whitespace character
Value must be a well‑formed email address
If the built‑in constraints are insufficient, you can create custom validation logic by implementing org.springframework.validation.Validator :
public class ConfigPropertiesValidator implements Validator {
@Override
public boolean supports(Class
clazz) {
return AppConfigProperties.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
AppConfigProperties config = (AppConfigProperties) target;
if (StringUtils.isEmpty(config.getId())) {
errors.rejectValue("id", "app.id.empty", "[app.id] property must be configured");
} else if (config.getId().length() < 5) {
errors.rejectValue("id", "app.id.short", "[app.id] length must be at least 5");
}
}
}Register the validator as a bean with the exact name configurationPropertiesValidator so that Spring Boot picks it up during startup:
@Bean
public ConfigPropertiesValidator configurationPropertiesValidator() {
return new ConfigPropertiesValidator();
}After adjusting the app.id value in application.yml , you will see either a successful start or the custom error messages defined above.
Summary : Enabling Spring Boot's startup validation using Java Validation annotations and optional custom validators allows early detection of mis‑configured parameters, reduces debugging effort, and provides a solid foundation for building reusable starter libraries that enforce required configuration such as a Redis key prefix.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.