所以我有两个参数由 Spring @Value
注释注入(inject)。 minLength
和 maxLength
。从逻辑上讲,minLength
不得高于 maxLength
,反之亦然。这是我尝试过但失败了。
抛出异常,maxLength为0
,但在我的application.properties
中我将其设置为7
(在验证之前测试)
private int minLength;
private int maxLength;
@Min(5)
@Value("${ com.manudcamera.webapi.uuid.minLength : 5} ")
private void setMinLength(int minLength) {
if ( minLength > this.maxLength ) throw new IllegalArgumentException("minimum length cannot be greater than maximum length | min len: " + minLength + " max len: " + this.maxLength);
this.minLength = minLength;
}
@Max(50)
@Value("${ com.manudcamera.webapi.uuid.maxLength : 15 }")
private void setMaxLength(int maxLength) {
if ( maxLength < this.minLength ) throw new IllegalArgumentException("maximum length cannot be greather than minimum length | min len: " + minLength + " max len: " + maxLength);
this.maxLength = maxLength;
}
最佳答案
提示 0 的是您的自定义验证代码,因为在调用 setMinavalue 时 maxLength 值尚未设置(因此为 0)。
一种可能的方法是在执行交叉验证的自定义方法上使用 PostConstruct 注释。
另一种可能的方法是实现您自己的自定义 validator (在类级别应用)来执行跨字段验证。
关于java - Spring @Value注入(inject)字段之间的注入(inject)字段验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45705460/