简体   繁体   English

Java ConstraintValidator 用途

[英]Java ConstraintValidator Purpose

I am trying to understand the purpose of Java's ConstraintValidator interface.我试图了解 Java 的ConstraintValidator接口的目的。

It's an interface, however how does it make coding more quick or more efficient?它是一个界面,但是它如何使编码更快或更高效? Trying to understand benefits of using it with our team.试图了解与我们的团队一起使用它的好处。

From Baeldung's Spring MVC Custom Validation :来自 Baeldung 的Spring MVC 自定义验证

The validation class implements the ConstraintValidator interface, and must also implement the isValid method;验证类实现了ConstraintValidator接口,还必须实现isValid方法; it's in this method that we defined our validation rules.正是在这个方法中,我们定义了我们的验证规则。

Naturally, we're going with a simple validation rule here in order to show how the validator works.自然地,我们在这里使用一个简单的验证规则来展示验证器是如何工作的。

ConstraintValidator defines the logic to validate a given constraint for a given object. ConstraintValidator 定义了为给定对象验证给定约束的逻辑。 Implementations must comply with the following restrictions:实现必须遵守以下限制:

Code Example:代码示例:

public class ContactNumberValidator implements 
  ConstraintValidator<ContactNumberConstraint, String> {

    @Override
    public void initialize(ContactNumberConstraint contactNumber) {
    }

    @Override
    public boolean isValid(String contactField,
      ConstraintValidatorContext cxt) {
        return contactField != null && contactField.matches("[0-9]+")
          && (contactField.length() > 8) && (contactField.length() < 14);
    }

}

The purpose is to define custom validation-logic for a custom annotation.目的是为自定义注释定义自定义验证逻辑。

Purpose of ConstraintValidator ConstraintValidator的目的

In the given example the ConstraintValidator implementation can be used as annotation on your property (assuming ContactNumberConstraint is a public @interface , defined as annotation for fields):在给定的示例中, ConstraintValidator实现可以用作属性的注释(假设ContactNumberConstraintpublic @interface ,定义为字段的注释):

@ContactNumberConstraint
String contactField;

As such shorthand it combines several validations like otherwise have to be listed separately:作为这样的简写,它结合了几个验证,否则必须单独列出:

@NotNull  // contactField != null
@Pattern(regexp="[0-9]+" )  // contactField.matches("[0-9]+")
@Length(min=9, max=13)  // (contactField.length() > 8) && (contactField.length() < 14)
String contactField;

Here the implemented ConstraintValidator is used to validate the property, for example if used as parameter (in a REST-controller, or any other validated method).这里实现的ConstraintValidator用于验证属性,例如,如果用作参数(在 REST 控制器或任何其他经过验证的方法中)。

How does it make coding more quick or more efficient?它如何使编码更快或更高效?

With this pair of annotation and validator you can simply declare a rather complex validation at any field or class by just annotating the field - by adding one line .使用这对注解和验证器,您可以在任何字段或类中简单地声明一个相当复杂的验证,只需对字段进行注解——通过添加一行 Spring would care about initiating the validation. Spring 会关心启动验证。 It executes the logic defined in validator and handles errors.它执行验证器中定义的逻辑并处理错误。

This predefined validation component as pair of annotation-interface and validator can be reused easily at many places (reduce code duplication), it can be composed and allows giving complex validations a name (using an expressive annotation name).这个预定义的验证组件作为注释接口和验证器对可以在许多地方轻松重用(减少代码重复),它可以组合并允许为复杂的验证命名(使用富有表现力的注释名称)。 Through its declarative way (annotation) it is loosely coupled.通过其声明方式(注释),它是松散耦合的。

More on benefits of Java Bean Validation (JSR 303, 380)更多关于Java Bean 验证的好处(JSR 303、380)

See also也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM