javax.validation:收到错误“找不到类型的验证器:" [英] javax.validation: Receive an error 'No validator could be found for type:'

查看:659
本文介绍了javax.validation:收到错误“找不到类型的验证器:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

框架:JQuery,Spring,Hibernate,Hibernate验证程序(JSR-303 Bean验证)
平台:Windows

Framework: JQuery, Spring, Hibernate, Hibernate Validator(JSR-303 Bean Validation)
Platform: Windows

我正在尝试使用JSR 303-Bean验证定义自定义约束@IdMustExist.约束的目的是检查关联表中是否存在输入的id值.我收到错误"javax.validation.UnexpectedTypeException:找不到类型为com.mycompany.myapp.domain.package1.class1的验证器".

I am trying to define a custom constraint @IdMustExist using JSR 303-Bean Validation. The purpose of the constraint is to check whether the entered id value exists in associated table. I am receiving an error 'javax.validation.UnexpectedTypeException: No validator could be found for type: com.mycompany.myapp.domain.package1.class1'.

如果将@IdMustExist放在Class1的字段定义上(如下面的示例代码所示),则会收到上述错误.但是,如果我在字符串字段上放置@IdMustExist约束,则不会收到上述错误.我的代码在IdMustExistValidator.java中崩溃.我不明白为什么Hibernate可以为"String"类找到验证器,而为域类"Class1"找到验证器.

If I place @IdMustExist on a field definition of Class1 (as given in example code below), I receive the above error. But If I place @IdMustExist constraint on a String field, I do not receive the above error. My code crashes in IdMustExistValidator.java. I dont understand why Hibernate can find Validator for 'String' class but not for domain class 'Class1'.

我的班级定义如下.

Class2.java

@Entity
@Table
public class Class2 extends BaseEntity {
/**
 * Validation: Class1 Id must exist. 
 */ 
@ManyToOne
@JoinColumn(name="Class1Id")
@IdMustExist(entity=Class1.class)
private Class1 class1;

..

IdMustExist.java

@Required
@Target( { METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = IdMustExistValidator.class)
@Documented
@ReportAsSingleViolation
public @interface IdMustExist {
String message() default "{com.mycompany.myapp.domain.validation.constraints.idmustexist}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};

/**
 * The entity class that contains the id property.
 */
Class<?> entity();

/**
 * The property of the entity we want to validate for existence. Default value is "id"
 */
String idProperty() default "id";
}

IdMustExistValidator.java(请注意,此类设计存在错误)

public class IdMustExistValidator implements ConstraintValidator<IdMustExist, Serializable> {

/**
 * Retrieve the entity class name and id property name
 */
public void initialize(IdMustExist idMustExist) {
    if (idMustExist.entity() != null) 
        entity = idMustExist.entity();
    idProperty = idMustExist.idProperty();
}

/**
 * Retrieve the entity for the given entity class and id property.
 * If the entity is available return true else false
 */
public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {
    logger.debug("Property Class = {}", property.getClass().getName());
    logger.debug("Property = {}", property);
    List resultList = commonDao.getEntityById(entity.getName(), idProperty);
    return resultList != null && resultList.size() > 0;
}

private Class<?> entity;
private String idProperty;

@Autowired
private CommonDao commonDao;
private final Logger logger = LoggerFactory.getLogger(IdMustExistValidator.class);

}

推荐答案

声明了约束验证器以验证Serializable值. 也许Class2不是Serializable?

Your constraint validator is declared to validate Serializable values. Perhaps Class2 is not Serializable?

这篇关于javax.validation:收到错误“找不到类型的验证器:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆