gpt4 book ai didi

java - 嵌套 ConfigurationProperties 中的 Spring 属性验证

转载 作者:行者123 更新时间:2023-12-02 02:56:30 25 4
gpt4 key购买 nike

我正在使用@ConfigurationProperties来跟踪我的属性。一切工作正常,包括子配置的类成员。 属性已正确加载,框架使用 getter 来确定“上下文的名称”。但是,我在尝试验证时遇到了麻烦,它不验证子属性。

<?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>

应用程序属性:

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);
}

}

主要配置属性,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 ...
}

子配置属性,SubProperties.java

@Component
@ConfigurationProperties
public class SubProperties
{
@Max(10)
private int subValue;

... getter & setter ...
}

因此具有给定属性文件的输出:

=========================================
Value: 4
Value2: 6
SubValue: 9
=========================================

如果我超出 Value 和 Value2 的范围,一切都很好(验证有效):

***************************
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

但是使用这些值:

test.value = 4
test.value2 = 6
test.sub.subValue = 11

验证不会触发(即使子属性有 @Max 约束)。

正常吗?

我错过了什么?

最佳答案

您错过了 @Valid 注释:

@Component
@ConfigurationProperties(prefix = "test")
public class TestProperties
{
@Min(4)
private int value;

@Max(6)
private int value2;

@Valid
private SubProperties sub;

... getters and setters ...
}

关于java - 嵌套 ConfigurationProperties 中的 Spring 属性验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com