如何使用Hibernate驗證器驗證構造函數或方法內的參數?我希望在創建ValueObject之前進行驗證,這樣我可以引發異常並且不會創建對象,除非所有參數都有效。Hibernate Validator方法或構造函數驗證
基本上我試圖使用,而不是如果可能的話做這樣的註解:
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;
}
}
我試過以下的docs這樣,這似乎工作,但仍然導致正在創建的對象。
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<Person>> violations = validator.validate(person);
if (violations.size() > 0) {
throw new IllegalArgumentException();
}
}
public void makeConditionalPerson() {
person2 = new ConditionalPerson(null, "", 123);
}
}
驗證:
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;
}
}