@ConfigurationProperties Validation
Spring Boot attempts to validate @ConfigurationProperties classes whenever they are annotated with Spring’s @Validated annotation.
You can use JSR-303 jakarta.validation constraint annotations directly on your configuration class.
To do so, ensure that a compliant JSR-303 implementation is on your classpath and then add constraint annotations to your fields, as shown in the following example:
- 
Java 
- 
Kotlin 
import java.net.InetAddress;
import jakarta.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@ConfigurationProperties("my.service")
@Validated
public class MyProperties {
	@NotNull
	private InetAddress remoteAddress;
	// getters/setters...
	public InetAddress getRemoteAddress() {
		return this.remoteAddress;
	}
	public void setRemoteAddress(InetAddress remoteAddress) {
		this.remoteAddress = remoteAddress;
	}
}import jakarta.validation.constraints.NotNull
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.validation.annotation.Validated
import java.net.InetAddress
@ConfigurationProperties("my.service")
@Validated
class MyProperties {
	var remoteAddress: @NotNull InetAddress? = null
}| You can also trigger validation by annotating the @Beanmethod that creates the configuration properties with@Validated. | 
To ensure that validation is always triggered for nested properties, even when no properties are found, the associated field must be annotated with @Valid.
The following example builds on the preceding MyProperties example:
- 
Java 
- 
Kotlin 
import java.net.InetAddress;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
@ConfigurationProperties("my.service")
@Validated
public class MyProperties {
	@NotNull
	private InetAddress remoteAddress;
	@Valid
	private final Security security = new Security();
	// getters/setters...
	public InetAddress getRemoteAddress() {
		return this.remoteAddress;
	}
	public void setRemoteAddress(InetAddress remoteAddress) {
		this.remoteAddress = remoteAddress;
	}
	public Security getSecurity() {
		return this.security;
	}
	public static class Security {
		@NotEmpty
		private String username;
		// getters/setters...
		public String getUsername() {
			return this.username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
	}
}import jakarta.validation.Valid
import jakarta.validation.constraints.NotEmpty
import jakarta.validation.constraints.NotNull
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.validation.annotation.Validated
import java.net.InetAddress
@ConfigurationProperties("my.service")
@Validated
class MyProperties {
	var remoteAddress: @NotNull InetAddress? = null
	@Valid
	val security = Security()
	class Security {
		@NotEmpty
		var username: String? = null
	}
}You can also add a custom Spring Validator by creating a bean definition called configurationPropertiesValidator.
The @Bean method should be declared static.
The configuration properties validator is created very early in the application’s lifecycle, and declaring the @Bean method as static lets the bean be created without having to instantiate the @Configuration class.
Doing so avoids any problems that may be caused by early instantiation.
| The spring-boot-actuatormodule includes an endpoint that exposes all@ConfigurationPropertiesbeans.
Point your web browser to/actuator/configpropsor use the equivalent JMX endpoint.
See the "Production ready features" section for details. |