How can I use Hibernate validator to validate the arguments inside the constructor or method? I want the validation to occur before the ValueObject creation so I can throw an exception and not create the object unless all parameters are valid.
Basically I'm trying to use annotations instead of doing something like this if possible:
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;
}
}
I've tried following the docs like this which seems to work but still results in the object being created.
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;
}
}
Adding the annotations to the constructor arguments seems to have no effect
public Person(@NotNull String name,
@NotNull String surname,
@Range(min=100, max=200) int age) {
...
}
How I'm creating the Objects:
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);
}
}
Validator:
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;
}
}
For anyone else that finds this post. I changed my approach slightly and got this working using OVal Validation & AspectJ instead of Hibernate.
Basically the same example as above except I needed to add @Guarded
above the class:
@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;
}
}
Then in your build.gradle add:
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)
}
You can do like this with hibernate validator using refection to validate the arguments:
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());
}
}
来源:https://stackoverflow.com/questions/43859895/hibernate-validator-method-or-constructor-validation