1.環境准備(Maven工程)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wy</groupId> <artifactId>validator_1</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.3.2.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project> View Code2.准備po類
package po; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; /** * Created by Administrator on 2016/7/12. */ public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; public Car(String manufacturer, String licencePlate, int seatCount) { this.manufacturer = manufacturer; this.licensePlate = licencePlate; this.seatCount = seatCount; } } View Code3.驗證測試
package test; import org.junit.BeforeClass; import org.junit.Test; import po.Car; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import java.util.Set; /** * Created by Administrator on 2016/7/12. */ public class CarTest { private static Validator validator; /*獲取驗證器*/ @BeforeClass public static void setUp() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } @Test public void test1() { Car car = new Car(null, "DD-AB-123", 4); //validate() 方法會返回一個set的 ConstraintViolation 的實例的集合, 我們可以通過遍歷它來查看哪些驗證錯誤 Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); System.out.println(constraintViolations.size()); System.out.println(constraintViolations); //[ConstraintViolationImpl{interpolatedMessage='不能為null', propertyPath=manufacturer, rootBeanClass=class po.Car, messageTemplate='{javax.validation.constraints.NotNull.message}'}] } @Test public void licensePlateTooShort() { Car car = new Car("Morris", "D", 4); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); System.out.println(constraintViolations.size()); System.out.println(constraintViolations); //[ConstraintViolationImpl{interpolatedMessage='個數必須在2和14之間', propertyPath=licensePlate, rootBeanClass=class po.Car, messageTemplate='{javax.validation.constraints.Size.message}'}] } @Test public void seatCountTooLow() { Car car = new Car("Morris", "DD-AB-123", 1); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); System.out.println(constraintViolations.size()); System.out.println(constraintViolations); //[ConstraintViolationImpl{interpolatedMessage='最小不能小於2', propertyPath=seatCount, rootBeanClass=class po.Car, messageTemplate='{javax.validation.constraints.Min.message}'}] } @Test public void carIsValid() { Car car = new Car("Morris", "DD-AB-123", 2); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); System.out.println(constraintViolations.size()); System.out.println(constraintViolations); //此時為一切正常 } } View Code