[英]how to execute validation on property annotated with @valid before a class level annotation?
Assume a class A associates class B, I need data validation between A and B, so I add a custom annotation on class A class level, also make it in a different group, so I can control the execution order, but it seems only work on A, I want the custom validation be executed after the basic validation done both on A and B, but it seems doesn't, the validation on B is executed after, how can make it before the custom validation?假设一个A类关联了B类,我需要在A和B之间进行数据验证,所以我在A类的类级别添加了一个自定义注解,也让它在不同的组中,这样我就可以控制执行顺序,但似乎只能工作在 A 上,我希望在 A 和 B 上完成基本验证之后执行自定义验证,但似乎没有,B 上的验证是在之后执行的,如何在自定义验证之前进行? Here are the sample codes:
以下是示例代码:
@XXXValidation(groups = Second.class)
@GroupSequence({A.class, Second.class})
public class A {
@valid
private B b;
}
public class B {
@NotNull
private String name;
}
Using @GroupSequence
on the class level re-defines the group sequence for the Default group.在类级别使用
@GroupSequence
重新定义默认组的组序列。 This is local to the class and is not propagated to any association.这是类本地的,不会传播到任何关联。 The Default group will be validated in the associated class.
默认组将在关联的类中进行验证。 See also the relevant chapter in the Bean Validation specification - http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence-redefiningdefaultgroup .
另请参阅 Bean 验证规范中的相关章节 - http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-groupsequence-redefiningdefaultgroup 。
@Valid is an orthogonal concept to the notion of group.
@Valid 是组概念的正交概念。 If two groups are in sequence, the first group must pass for all associated objects before the second group is evaluated.
如果两个组按顺序排列,则在评估第二个组之前,第一个组必须通过所有关联的对象。 Note however that the Default group sequence overriding is local to the class it is defined on and is not propagated to the associated objects.
但是请注意,默认组序列覆盖对于定义它的类是本地的,不会传播到关联的对象。
If you want to make sure that the whole object graph is validated in a given group order, then you need to "request" this order as part of the top level call to Validator.validate .如果您想确保以给定的组顺序验证整个对象图,那么您需要“请求”这个顺序作为对Validator.validate的顶级调用的一部分。 If you control validation yourself, this should be straight forward.
如果您自己控制验证,这应该是直截了当的。 If you are using Bean Validation as part of another framework, you will need to refer to its documentation on how to specify groups.
如果您将 Bean Validation 用作另一个框架的一部分,则需要参考其有关如何指定组的文档。 JPA for example defines a set of properties which can be set (eg in persistence.xml ) in order to define which group(sequence)s should be evaluated as part JPA life cycle events.
例如,JPA 定义了一组可以设置的属性(例如,在persistence.xml 中),以便定义应该将哪些组(序列)作为 JPA 生命周期事件的一部分进行评估。
Depending on your use case you might be able to use @ConvertGroup as well.根据您的用例,您也可以使用@ConvertGroup 。 Check the specification or online documentation for Hibernate Validator for examples.
查看 Hibernate Validator 的规范或在线文档以获取示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.