Validate @ Value-annotated fields during application launch
How do I check that the values ββin the properties file match my expectations when starting the application? Field annotation doesn't work directly. I am using spring boot 1.5.3.RELEASE, spring 4.3.8.RELEASE
Services:
@Service
@Validated
public class ConfigService {
@URL
@Value("${checkout.url}")
private String checkoutUrl;
@Size(max = 26)
@Value("${checkout.payment-method}")
private String paymentMethod;
}
Property file (application.properties):
checkout.url=not-a-url-at-all
checkout.payment-method=CreditCardButMuchTooLongToQualifyForSizeValidator
+3
source to share
1 answer
You can use this getter method in your class to check string length:
@Size(min = Number, max = Number)
public String getField() {
return field;
}
and to check the numbers:
@Max(Number) or @Min(Number)
When number is equal to int
if the fields do not have these restrictions, he will throw java.lang.IllegalStateException
Don't forget to add this dependency to your pom if you don't have:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
+1
source to share