class="java"> package com.chinacache.entity; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import org.springframework.validation.annotation.Validated; @Validated public class Person { @NotNull private String name; @Min(0) @Max(100) private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String[] args) { Person p = new Person(); p.setAge(19999); ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<Person>> validate = validator.validate(p); for (ConstraintViolation<Person> constraintViolation : validate) { System.out.println(constraintViolation.getPropertyPath()); System.out.println(constraintViolation.getMessage()); } } }