Spring Boot Configurationproperties Validate

Source: Internet
Author: User

24.7.4 @ConfigurationProperties Validation

Spring Boot'll attempt to validate external configuration by default using JSR-303 (if it's on the classpath).
You can simply add JSR-303 javax.validation constraint annotations to your @ConfigurationProperties class:

@ConfigurationProperties (prefix= "foo")publicclass  fooproperties {    @NotNull     Private inetaddress remoteaddress;     // . .. getters and Setters }

In order to validate values of nested properties, you must annotate the associated field as to @Valid trigger its Validati On. For example, building upon the above FooProperties example:

@ConfigurationProperties (prefix= "Connection") Public classfooproperties {@NotNullPrivateinetaddress remoteaddress; @ValidPrivate FinalSecurity Security =NewSecurity (); //. .. getters and Setters     Public Static classSecurity {@NotEmpty PublicString username; //. .. getters and Setters    }}

You can also add a custom Spring by Validator 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 allows the bean to being created without has to instantiate the @Configuration class. This avoids any problems the May is caused by early instantiation. There is a property validation sample so you can see how to set things up.

Spring Boot Configurationproperties Validate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.