Merge pull request #3566 from drumonii/gh-3510-doc
* pr/3566: Polish Document validation of nested components
This commit is contained in:
commit
9cd3b20a78
|
|
@ -781,6 +781,35 @@ annotations to your `@ConfigurationProperties` class:
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
|
||||||
|
In order to validate values of nested properties, you must annotate the associated field
|
||||||
|
as `@Valid` to trigger its validation. For example, building upon the above
|
||||||
|
`ConnectionSettings` example:
|
||||||
|
|
||||||
|
[source,java,indent=0]
|
||||||
|
----
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix="connection")
|
||||||
|
public class ConnectionSettings {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Valid
|
||||||
|
private RemoteAddress remoteAddress;
|
||||||
|
|
||||||
|
// ... getters and setters
|
||||||
|
|
||||||
|
private static class RemoteAddress {
|
||||||
|
|
||||||
|
@NotEmpty
|
||||||
|
public String hostname;
|
||||||
|
|
||||||
|
// ... getters and setters
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
You can also add a custom Spring `Validator` by creating a bean definition called
|
You can also add a custom Spring `Validator` by creating a bean definition called
|
||||||
`configurationPropertiesValidator`. There is a
|
`configurationPropertiesValidator`. There is a
|
||||||
{github-code}/spring-boot-samples/spring-boot-sample-property-validation[Validation sample]
|
{github-code}/spring-boot-samples/spring-boot-sample-property-validation[Validation sample]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue