在Hibernate Validator中验证

时间:2014-01-06 17:00:29

标签: java hibernate validation nhibernate-validator

我想使用Hibernate Validator,我在第一步检查并研究了以下参考资料:

第3页的第一个样本是我的问题

我没有找到这个导入包(javax.validation。*)

在hibernate-validator-5.0.2.Final.jar

我发现只有(org.hibernate.validator。*),但这些类不存在

package org.hibernate.validator.referenceguide.chapter01;

import java.util.Set;
// ?????
import javax.validation.ConstraintViolation; // Not Found 
import javax.validation.Validation; // Not Found 
import javax.validation.Validator; // Not Found 
import javax.validation.ValidatorFactory; // Not Found 
// ?????
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CarTest {

    private static Validator validator;

    @BeforeClass
    public static void setUp() {
        // ?????
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
        // ?????
    }

    @Test
    public void manufacturerIsNull() {
        Car car = new Car(null, "DD-AB-123", 4);
        Set<ConstraintViolation<Car>> constraintViolations
                = validator.validate(car);
        assertEquals(1, constraintViolations.size());
        assertEquals("may not be null", constraintViolations.iterator().next().getMessage());

}

请帮助我并指导以下代码的必要软件包?

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    validator = factory.getValidator();

1 个答案:

答案 0 :(得分:1)

您需要将validation-api jar添加到类路径中。最新版本为validation-api-1.1.0.Final.jar。类javax.validation.*包含在这个jar中。