简体   繁体   English

如何在类级别注释之前对用@valid 注释的属性执行验证?

[英]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.

相关问题 AspectJ-在指定方法处的切入点,其参数带有类级别的注释 - AspectJ - Pointcut at specified method with a param annotated with class level annotation 使用AOP进行注释-在方法级别上起作用,而仅对类进行注释 - Annotation with AOP - works at method level while only the class is annotated 自定义类级别验证 - 创建注释 - Custom Class Level Validation - Creating the Annotation 实例化带有一些注释的类 - Instantiate a Class annotated with some annotation 如何在注释处理过程中获取带注释的类类型以生成代码? - How to retrieve class type of annotated during annotation processing for code generation? 如何将默认值设置为注释变量作为注释变量的类类型? - How to set default value to annotation variable as the class type of the variable annotated? DTO 级别基于注释的验证发生在字段级别验证之前,例如 @NotNull @Size 等 - Annotation based validation on DTO level happens before field level validation like @NotNull @Size etc 如何排除对使用特定注释注释但未使用其他注释注释的bean的扫描? - How to exclude scanning of beans that annotated with specific annotation and not annotated with another annotation? 在Java类级注释中分配属性值 - Assign a property value in a Java class-level annotation SpringBoot:没有@Valid注解的验证 - SpringBoot: Validation without @Valid annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM