Hibernate Validator方法或构造函数validation

如何使用Hibernatevalidation器validation构造函数或方法中的参数? 我希望validation在ValueObject创建之前发生,这样我就可以抛出exception而不创建对象,除非所有参数都有效。

基本上我正在尝试使用注释,而不是像这样做,如果可能的话:

public class ConditionalPerson { private String name; private String surname; private int age; public ConditionalPerson(String name, String surname, int age){ if (name == null || surname == null || age < 1) { throw new IllegalArgumentException(); } this.name = name; this.surname = surname; this.age = age; } } 

我已经尝试过像这样的文档似乎可以工作,但仍然会导致创建对象。

 public class Person { @NotNull(message = "Name can't be null") @NotEmpty(message = "Name can't be empty") @Length(min=1) private String name; @NotNull(message = "Surname can't be null") @NotEmpty(message = "Surname can't be empty") @Length(min=1) private String surname; @Range(min=100, max=200) private int age; public Person(String name, String surname, int age){ this.name = name; this.surname = surname; this.age = age; } } 

将注释添加到构造函数参数似乎没有任何效果

 public Person(@NotNull String name, @NotNull String surname, @Range(min=100, max=200) int age) { ... } 

我是如何创建对象的:

 public class Example { Person person; ConditionalPerson person2; public static void main(String[] args) { Example example = new Example(); example.makePerson(); example.makeConditionalPerson(); } public void makePerson() { person = new Person(null, "", 12); Validator validator = ValidatorSingleton.getValidator(); Set<ConstraintViolation> violations = validator.validate(person); if (violations.size() > 0) { throw new IllegalArgumentException(); } } public void makeConditionalPerson() { person2 = new ConditionalPerson(null, "", 123); } } 

validation器:

 public class ValidatorSingleton { private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); private static final javax.validation.Validator validator = factory.getValidator(); private ValidatorSingleton(){} public static Validator getValidator() { return validator; } } 

对于找到这篇文章的其他人。 我略微改变了我的方法,并使用OVal Validation和AspectJ代替Hibernate。

基本上与上面相同的例子,除了我需要在类之上添加@Guarded

 @Guarded public class Person { private String name; private String surname; private int age; public Person(@NotNull String name, @NotNull String surname, @Range(min=100, max=200) int age){ this.name = name; this.surname = surname; this.age = age; } } 

然后在build.gradle中添加:

 buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'org.aspectj:aspectjtools:1.8.10' } } dependencies { compile 'org.aspectj:aspectjrt:1.8.1' compile 'net.sf.oval:oval:1.86' } tasks.withType(JavaCompile) { doLast { String[] args = ["-showWeaveInfo", "-1.8", "-inpath", destinationDir.toString(), "-aspectpath", classpath.asPath, "-d", destinationDir.toString(), "-classpath", classpath.asPath] MessageHandler handler = new MessageHandler(true); new Main().run(args, handler) } 

您可以使用hibernatevalidation器使用refection来validation参数:

 public class PersonTest { private static ExecutableValidator executableValidator; @BeforeClass public static void setUp() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); executableValidator = factory.getValidator().forExecutables(); } @Test public void test() throws NoSuchMethodException { Constructor constructor = Person.class.getConstructor(String.class, String.class, int.class); Set> violations = executableValidator.validateConstructorParameters(constructor, new Object[]{null, "", 12}); assertEquals(2, violations.size()); } }