gpt4 book ai didi

java - 如何告诉 hibernate validator 仅验证一个注释?

转载 作者:行者123 更新时间:2023-12-02 09:25:09 25 4
gpt4 key购买 nike

是否可以告诉 hibernate 验证仅验证一个特定注释?
我已经创建了使用 javax.validation.ConstraintValidator 的自定义注释,到目前为止一切顺利。
但是,当我想使用

手动验证对象时
javax.validation.Validator.validate(myOjbect);

它将验证所有注释,包括@NotNull、@Size...是否有一种优雅的方法来覆盖此行为,以便我可以选择要验证的注释?

最佳答案

我无法找到一种方法来限制对特定注释的验证而不修改已验证的类。您可以通过调用 Validator##validateProperty(object, propertyName, ...) 将其限制为属性但相当于将其显式限制为一组注释需要 assign groups to annotations :

似乎有效的是:

  1. 为您要验证的每组注释创建一个界面。
  2. 指定 groups 的接口(interface)每个注释的属性都要与该组进行验证。如果默认情况下还必须验证注释,请添加 Default接口(interface)。
  3. 将接口(interface)添加到 validator 调用。

一个例子:

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.groups.Default;

public class SomeTest {

public static void main(String[] args) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();

Cat a = new Cat(null);
// invalid: name is null
output(validator.validate(a), a);

Cat b = new Cat("?");
// invalid: name too short
output(validator.validate(b), b);

// valid: only @NotNull is evaluated, @Size does not matter.
output(validator.validate(b, NotNullName.class), b);

// invalid: only @NotNull is evaluated, @Size does not matter. Name is null.
output(validator.validate(a, NotNullName.class), a);
}

/**
* Output validation result.
*
* @param validate
* the validate
*/
private static void output(Set<ConstraintViolation<Cat>> validationResult, Cat cat) {
if (validationResult.isEmpty()) {
System.out.println(cat + " is valid!");
} else {
System.out.println(cat + " is invalid!\n" + validationResult);
}
}

// no need to implement an interface - just name it for annotation groups attribute:
public static class Cat {
@NotNull(groups = { NotNullName.class, Default.class })
@Size(min = 3, max = 45)
private String name;

public Cat(String name) {
super();
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Cat [");
if (name != null) {
builder.append("name=");
builder.append(name);
}
builder.append("]");
return builder.toString();
}

}

public static interface NotNullName {
// no members needed here
}
}

输出:

Cat [] is invalid!
[ConstraintViolationImpl{interpolatedMessage='darf nicht null sein', propertyPath=name, rootBeanClass=class SomeTest$Cat, messageTemplate='{javax.validation.constraints.NotNull.message}'}, ConstraintViolationImpl{interpolatedMessage='darf nicht null sein', propertyPath=name, rootBeanClass=class SomeTest$Cat, messageTemplate='{javax.validation.constraints.NotNull.message}'}]
Cat [name=?] is invalid!
[ConstraintViolationImpl{interpolatedMessage='Länge muss zwischen 3 und 45 liegen', propertyPath=name, rootBeanClass=class SomeTest$Cat, messageTemplate='{javax.validation.constraints.Size.message}'}]
Cat [name=?] is valid!
Cat [] is invalid!
[ConstraintViolationImpl{interpolatedMessage='darf nicht null sein', propertyPath=name, rootBeanClass=class SomeTest$Cat, messageTemplate='{javax.validation.constraints.NotNull.message}'}]

关于java - 如何告诉 hibernate validator 仅验证一个注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58391875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com