Hibernate Validator 自定义约束不起作用
Hibernate Validator custom constraint not working
我正在尝试使用 Hibernate Validator 创建自定义约束。
如果我使用标准的,如@Size,它运行良好并且我收到了错误消息。
但是,我定制的那个不起作用,我不知道为什么。
这是我的代码:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = TestValidateur.class)
public @interface ObjectValid {
String message() default "test";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
并且:
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class TestValidateur implements ConstraintValidator<ObjectValid , String> {
public void initialize(ObjectValid constraintAnnotation) {
}
public boolean isValid(String NoObject, ConstraintValidatorContext ctx) {
return false ;
}
}
@Column(name="NoObject")
@ObjectValid
@Size(min = 4, max = 14)
public String NoObject;
并测试验证:
import java.io.FileNotFoundException;
import java.util.Set;
import javax.persistence.EntityManager;
import org.hibernate.mapping.Component;
import org.hibernate.validator.HibernateValidator;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
public class NoTest {
private static Validator validator;
public static void main(String[] args) throws FileNotFoundException, NoSuchMethodException, SecurityException {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Object objectVerif = new Object("ss");
Set<ConstraintViolation<Object>> validationErrors = validator.validate(objectVerif);
if(!validationErrors.isEmpty()){
for(ConstraintViolation<Object> error : validationErrors){
System.out.println(error);
}
}
}
}
如果我 运行 应用程序,我只会收到 @Size
的错误消息
ConstraintViolationImpl{interpolatedMessage='la taille doit être comprise entre 4 et 14', propertyPath=NoObject, rootBeanClass=class tp03.Object, messageTemplate='{jakarta.validation.constraints.Size.message}'}
如果有人知道我遗漏了什么,将不胜感激
谢谢
验证对象:
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Size;
@Entity
@Table(name="object",indexes = { @Index(name="primary",columnList = "idObject")})
public class Object {
@Id
@GeneratedValue
@Column(name="idObject")
private int idObject;
public int getidObject()
{
{return idObject;}
}
public void SetidObject(int idObject)
{
this.idObject=idObject;
}
@Column(name="NoObject")
@ObjectValid
@Size(min = 4, max = 14)
public String NoObject;
public String getNoObject()
{
{return NoObject;}
}
public void SetNoObject(String NoObject)
{
this.NoObject=NoObject;
}
public Object(String NoObject)
{this.NoObject=NoObject;
}
}
这里的问题是javax.validation.Constraint
用于定义自定义约束,而jakarta.validation.Validator
用于执行验证,然后只验证jakarta.validation.Constraint
s。
如果您在 ObjectValid
约束界面和 TestValidateur
class 中将导入从 javax
更改为 jakarta
,则两个约束都会正确验证。
我正在尝试使用 Hibernate Validator 创建自定义约束。 如果我使用标准的,如@Size,它运行良好并且我收到了错误消息。 但是,我定制的那个不起作用,我不知道为什么。
这是我的代码:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ ElementType.FIELD, ElementType.PARAMETER })
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = TestValidateur.class)
public @interface ObjectValid {
String message() default "test";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
并且:
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class TestValidateur implements ConstraintValidator<ObjectValid , String> {
public void initialize(ObjectValid constraintAnnotation) {
}
public boolean isValid(String NoObject, ConstraintValidatorContext ctx) {
return false ;
}
}
@Column(name="NoObject")
@ObjectValid
@Size(min = 4, max = 14)
public String NoObject;
并测试验证:
import java.io.FileNotFoundException;
import java.util.Set;
import javax.persistence.EntityManager;
import org.hibernate.mapping.Component;
import org.hibernate.validator.HibernateValidator;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
public class NoTest {
private static Validator validator;
public static void main(String[] args) throws FileNotFoundException, NoSuchMethodException, SecurityException {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Object objectVerif = new Object("ss");
Set<ConstraintViolation<Object>> validationErrors = validator.validate(objectVerif);
if(!validationErrors.isEmpty()){
for(ConstraintViolation<Object> error : validationErrors){
System.out.println(error);
}
}
}
}
如果我 运行 应用程序,我只会收到 @Size
的错误消息ConstraintViolationImpl{interpolatedMessage='la taille doit être comprise entre 4 et 14', propertyPath=NoObject, rootBeanClass=class tp03.Object, messageTemplate='{jakarta.validation.constraints.Size.message}'}
如果有人知道我遗漏了什么,将不胜感激
谢谢
验证对象:
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import jakarta.validation.constraints.Size;
@Entity
@Table(name="object",indexes = { @Index(name="primary",columnList = "idObject")})
public class Object {
@Id
@GeneratedValue
@Column(name="idObject")
private int idObject;
public int getidObject()
{
{return idObject;}
}
public void SetidObject(int idObject)
{
this.idObject=idObject;
}
@Column(name="NoObject")
@ObjectValid
@Size(min = 4, max = 14)
public String NoObject;
public String getNoObject()
{
{return NoObject;}
}
public void SetNoObject(String NoObject)
{
this.NoObject=NoObject;
}
public Object(String NoObject)
{this.NoObject=NoObject;
}
}
这里的问题是javax.validation.Constraint
用于定义自定义约束,而jakarta.validation.Validator
用于执行验证,然后只验证jakarta.validation.Constraint
s。
如果您在 ObjectValid
约束界面和 TestValidateur
class 中将导入从 javax
更改为 jakarta
,则两个约束都会正确验证。