我正在尝试使用 Constraint composition并希望为每个复合约束定义组,如下例所示:-
复合约束
@Target({
ElementType.FIELD,
ElementType.METHOD,
ElementType.PARAMETER,
ElementType.TYPE,
ElementType.ANNOTATION_TYPE
})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@DummyValid1(group = Group1.class)
@DummyValid2(group = Group2.class)
@Constraint(validatedBy = {})
public @interface DummyCompositeValid {
String message() default "The bean is invalid.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
数据bean
@DummyCompositeValid
public class DummyBean {
//Fields declaration, Setter and Getter
}
业务验证方法
this.validator.validate(dummyBean, Group1.class, Group2.class);
结果是 dummyBean 没有被验证。我很困惑,因为当组在没有复合约束的情况下直接定义时运行良好,如下所示:-
数据bean
@DummyValid1(group = Group1.class)
@DummyValid2(group = Group2.class)
public class DummyBean {
//Fields declaration, Setter and Getter
}
能否请您进一步提供建议和解释?非常感谢您的提前帮助。我期待着尽快收到您的来信。
问候,
查理查
最佳答案
我已经向 Hibernate validator 论坛提出了这个问题,并希望将结果告知我们如下:-
我想做的是不可能的。组合约束从主注释继承组。这是规范的一部分:
Groups from the main constraint annotation are inherited by the composing annotations. Any groups definition on a composing annotation is ignored. Likewise, payload from the main constraint annotation is inherited by the composing annotations. Any payload definition on a composing annotation is ignored.
关于java - JSR 303 Bean 验证 : The Constraint composition with groups for each composited constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11045198/