如何使用Hibernate验证器验证构造函数或方法中的参数?我希望验证在ValueObject创建之前发生,这样我就可以抛出异常而不创建对象,除非所有参数都有效。
基本上我试图使用注释而不是像这样做这样的事情:
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;
}
}
答案 0 :(得分:2)
对于发现此帖子的其他人。 我略微改变了我的方法,并使用OVal Validation&amp; 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)
}
答案 1 :(得分:0)
你可以使用hibernate验证器使用refection来验证参数:
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<Person> constructor =
Person.class.getConstructor(String.class, String.class, int.class);
Set<ConstraintViolation<Person>> violations =
executableValidator.validateConstructorParameters(constructor, new Object[]{null, "", 12});
assertEquals(2, violations.size());
}
}