Skip to content

Commit 9cd3b20

Browse files
committed
Merge pull request #3566 from drumonii/gh-3510-doc
* pr/3566: Polish Document validation of nested components
2 parents 9244781 + 34891c2 commit 9cd3b20

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,35 @@ annotations to your `@ConfigurationProperties` class:
781781
}
782782
----
783783

784+
785+
In order to validate values of nested properties, you must annotate the associated field
786+
as `@Valid` to trigger its validation. For example, building upon the above
787+
`ConnectionSettings` example:
788+
789+
[source,java,indent=0]
790+
----
791+
@Component
792+
@ConfigurationProperties(prefix="connection")
793+
public class ConnectionSettings {
794+
795+
@NotNull
796+
@Valid
797+
private RemoteAddress remoteAddress;
798+
799+
// ... getters and setters
800+
801+
private static class RemoteAddress {
802+
803+
@NotEmpty
804+
public String hostname;
805+
806+
// ... getters and setters
807+
808+
}
809+
810+
}
811+
----
812+
784813
You can also add a custom Spring `Validator` by creating a bean definition called
785814
`configurationPropertiesValidator`. There is a
786815
{github-code}/spring-boot-samples/spring-boot-sample-property-validation[Validation sample]

0 commit comments

Comments
 (0)