java - JSR 303 Bean 验证 : The Constraint composition with groups for each composited constraint

标签 java bean-validation

我正在尝试使用 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/

相关文章:

java - SQL CODE -104(错误)可以代表什么?

java - 使用 JSP 包含指令、JSP 包含操作和使用 JSP 标记文件包含文件有什么区别?

java - 无法解析 Integer.ParseInt 抛出的 java.lang.NumberFormatException

java - 验证期间的 Websphere Liberty ClassNotFoundException

spring - 自定义 spring 验证错误

java - 用于代理收集的 Hibernate validator

java - Spring登录用户信息检索

java - 观察者模式: delay action after event trigger

java - JSR303 : Is it possible to inject arguments to a custom validator at validation time?

java - 您可以在运行时更改注释消息吗?