0
我想用Hibernate驗證,在最初的步驟我檢查,並研究了以下參考:驗證在Hibernate驗證
在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();
我可以在沒有這個jar的情況下使用Hibernate驗證嗎?或者我必須有它的任何休眠驗證? – user3166171
不,這是必需的。它包含Bean Validation API,而Hibernate Validation是這個API的一個實現。 –