I'm using @ConfigurationProperties
to keep track of my properties. Everything work fine, including class member for sub-configuration. Properties
are well loaded and the framework uses the getter to determine the "name of the context". However I have trouble when trying validation, it does not validate sub properties.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties:
test.value = 4
test.value2 = 6
test.sub.subValue = 9
ValidationApplication.java
@SpringBootApplication
public class ValidationApplication implements CommandLineRunner
{
private final TestProperties properties;
public ValidationApplication(TestProperties properties) {
this.properties = properties;
}
@Override
public void run(String... args) {
System.out.println("=========================================");
System.out.println("Value: " + this.properties.getValue());
System.out.println("Value2: " + this.properties.getValue2());
System.out.println("SubValue: " + this.properties.getSub().getSubValue());
System.out.println("=========================================");
}
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(ValidationApplication.class).run(args);
}
}
Main ConfigurationProperties, TestProperties.java
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties
{
@Min(4)
private int value;
@Max(6)
private int value2;
private SubProperties sub;
... getters and setters ...
}
Sub ConfigurationProperties, SubProperties.java
@Component
@ConfigurationProperties
public class SubProperties
{
@Max(10)
private int subValue;
... getter & setter ...
}
So the output with the given properties file:
=========================================
Value: 4
Value2: 6
SubValue: 9
=========================================
If I go out of range with Value and Value2, all is fine (validation works):
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target be.test.TestProperties@6cb6decd failed:
Property: test.value2
Value: 7
Reason: must be less than or equal to 6
Property: test.value
Value: 2
Reason: must be greater than or equal to 4
Action:
Update your application's configuration
With these values however:
test.value = 4
test.value2 = 6
test.sub.subValue = 11
The validation does not trigger (even if there is a @Max
constraint on sub property).
Is it normal?
What did I miss?
You missed a @Valid
annotation:
@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties
{
@Min(4)
private int value;
@Max(6)
private int value2;
@Valid
private SubProperties sub;
... getters and setters ...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.